arrays - 如何在 Go 中的 slice 或结构中存储不同的结构(不嵌入)

标签 arrays go struct slice

我刚接触Golang,在我参观了A Tour of Go之后,我正在尝试制作自己的东西。

我想要什么

我想将不同类型的结构放入单个 slice (或结构?),

所以我可以使用 for 循环来将每个结构传递给函数

例如

在 PHP 中,我可以将我的类存储在一个数组中,并将它们中的每一个传递给 foobar(),如下所示:

$classes = [$A, $B, $C, $D];  // $A, $B, $C, $D are classes (called `struct` in Golang).

foreach ($classes as $class)
    foobar($class);

我尝试了什么

我尝试在 Golang 中做同样的事情,我希望它看起来像这样:

A{B{}, C{}, D{}}

由于我在使用slice 时失败了,所以我决定使用struct 来保存我的structs:

type A struct {
    B
    C
    D
}

type B struct {
    Date string
}

type C struct {
    Date string
}

type D struct {
    Date string
}

func main() {   
    // Using reflect to loop the A struct: 
    // http://stackoverflow.com/questions/18926303/iterate-through-a-struct-in-go
    v := reflect.ValueOf(A{})

    for i := 0; i < v.NumField(); i++ {
        foobar(v.Field(i).Interface()) // Passing each struct to the `foobar()` function
    }
}

但看起来我实际上是在嵌入而不是存储它们,请问有什么建议吗?

最佳答案

我认为 interface{} 是您所追求的。例如像这样:

type A struct {
    data string
}

type B struct {
     data int
}

func printData(s interface{}) {
     switch s.(type) {
         case A:
             fmt.Printf("A: %s\n", s.(A).data)
         case B:
             fmt.Printf("B: %d\n", s.(B).data)
     }
 }

 func main() {
     classes := []interface{}{A{data: "first"}, B{data: 2}, A{data: "third"}}
     for _, c := range classes {
         printData(c)
     }
 }

这可能是您在 go 中最接近“鸭子打字”的程度。

尽管如果您的结构真的那么相似,您最好还是定义一个公共(public)接口(interface)并为每个结构实现该接口(interface)。这是使用接口(interface)的相同示例:

type DataGetter interface {
    getDate() string
}

type A struct {
    data string
}

func (a A) getDate() string {
    return a.data
}

type B struct {
    data int
}

func (b B) getDate() string {
    return fmt.Sprintf("%d", b.data)
}

func printData(s DataGetter) {
    fmt.Printf("%s\n", s.getDate())
}

func main() {

    classes := []DataGetter{A{data: "first"}, B{data: 2}, A{data: "third"}}
    for _, c := range classes {
        printData(c)
    }
}

关于arrays - 如何在 Go 中的 slice 或结构中存储不同的结构(不嵌入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39801833/

相关文章:

go - SSH 握手提示缺少主机 key

c++ - 将消息解析为结构

java - 如何在 Java 中使用枚举作为数组下标

java - Java 中 byte[] 的数组列表

arrays - 有没有办法在 Bash 中将一个字符串数组拆分为多个其他数组?

c++ - 调试中的 Eclipse CDT View 结构

C - 打印出现次数最多的字符串

javascript - 如何在我的文本冒险中的 If/Else 语句中打印正确的随机数组值?

json - 在 JSON 中转义逗号

用于 Tibco EMS 的 API