我是Golang的新手 当我尝试它时,我得到一个编译错误:
cannot use a.B (type []*C) as type []Z in field value
代码:
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)
}
我想要绕过的方式是迭代切片并将其分配给另一个切片。但我知道必须有其他方法来做到这一点。