bn0*_*00d 1 casting type-conversion go protocol-buffers grpc
我是 Golang 新手。
当我尝试它时,出现编译错误:
cannot use a.B (type []*C) as type []Z in field value
Run Code Online (Sandbox Code Playgroud)
代码:
package main
type A struct {
B []*C
}
type C struct {
char string
}
type X struct {
Y []Z
}
type Z struct {
char string
}
func doSomething(r interface{}) X {
a := r.(*A)
return X{
Y: a.B, // cannot use a.B (type []*C) as type []Z in field value
}
}
func main() {
something := &C{"abc"}
somewhere := A{}
somewhere.B = []*C{something}
doSomething(somewhere)
}
Run Code Online (Sandbox Code Playgroud)
我想解决的方法是迭代该切片并将其分配给另一个切片。但我知道一定还有其他方法可以做到。
| 归档时间: |
|
| 查看次数: |
7109 次 |
| 最近记录: |