简体   繁体   English

如何在切片中存储不同结构或在Go中存储结构(不嵌入)

[英]How do I STORE different structs in a slice or a struct in Go (Not embedding)

I am new to Golang, after I took a tour of A Tour of Go , I'm trying to make my own thing. 我是Golang的新手,在我参观A Tour of Go之后 ,我正在尝试打造自己的东西。

What I want 我想要的是

I want to put different types of the structs into a single slice (or struct?), 我想将不同类型的结构放入单个切片(或结构?)中,

so I can use a for loop to pass each struct to a function . 所以我可以使用for循环将每个结构传递给函数

For example 例如

In PHP I can store my classes in an array and pass each of them to foobar() like this: 在PHP中,我可以将类存储在数组中,并将每个类传递给foobar()如下所示:

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

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

What I tried 我尝试了什么

I tried to do the same in Golang, and I hope it looks like this: 我试图在Golang中做同样的事情,希望它看起来像这样:

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

Since I failed on using slice , I decided to use struct to hold my structs : 由于无法使用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
    }
}

But it seems like I'm actually doing embedding instead of storing them, any suggestions please? 但是似乎我实际上是在进行嵌入而不是存储它们,请问有什么建议吗?

I think interface{} is what you're after. 我认为interface {}是您所追求的。 For example like this: 例如这样:

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)
     }
 }

This is probably as close as you can get to "duck typing" in go. 这可能与您进行“鸭式打字”一样近。

Though if your structs are really that similar, you might better off defining a common interface instead and implementing that interface for each struct. 尽管如果您的结构确实如此相似,则最好还是定义一个通用接口,并为每个结构实现该接口。 Here is same example using interfaces: 这是使用接口的相同示例:

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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM