【发布时间】:2017-12-18 23:58:38
【问题描述】:
我有以下:
https://play.golang.org/p/q2NUMzbw6-
package main
import "fmt"
type A struct {
Name string
Address string
}
type B struct {
A
}
type C struct {
A
}
type D struct {
A
}
//....more structs that embed A
type myinterface interface {
SetName(string)
SetAddress(string)
}
func run() *A {
// iterate over a slice of structs that embed A.... how????
for _, s := range []*A{
&B{}, &C{}, &D{},
} {
s.SetName("Bob")
s.SetAddress("Maine")
// do some other stuff that gets very verbose w/out a slice...
return s.A
}
}
func main() {
a := run()
fmt.Println(a)
}
我需要遍历所有嵌入 A 的结构,但我很难这样做。以上不起作用“不能将 B 文字(类型 *B)用作数组或切片文字中的 *A 类型”。最好的方法是什么?
【问题讨论】:
标签: go