繁体   English   中英

如何在go中订购不同结构类型的列表

[英]How to order a list of different struct types in go

我只和Go一起工作了几天。 我定义了各种不同的结构类型,每个结构类型都包含一个日期。

不知何故,我需要按日期顺序处理这些结构,但该顺序必须跨越多种不同的结构类型。 在像Python这样的动态类型语言中,很容易创建一个由日期键入的所有对象的哈希值(如果它们不是唯一的,则列表的哈希值)。 在C中,我可以使用指针联合或void *。 但是我对Go中的这个问题感到困惑。

我想我可以保留每种类型的排序列表,并在我去的时候进行手动合并。 似乎klunky?

我读到的关于处理这种情况的内容似乎指向使用接口,但我并没有真正看到如何在这种情况下使用它们。

为了争论,让我说我有类似的东西:

type A struct {
    Date string
    Info string
}

type B struct {
    Date string
    Info int
}

(虽然在实践中有更多的结构,并且它们在多个字段中更复杂),并且只需要按日期顺序打印每个结构的(未排序的)数组的内容。

有没有办法为非统一对象类型创建列表(日期,指针)对?


根据以下第一个建议:

package main
import "fmt"

type A struct {
    Date string
    Info string
}
func (x *A) GetDate() string {
    return x.Date
}
type B struct {
    Date string
    Info int
}
func (x *B) GetDate() string {
    return x.Date
}

type Dater interface {
    GetDate() string
}

type Daters []Dater
func (s Daters) Len() int      { return len(s) }
func (s Daters) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type ByDate struct{ Daters }
func (s ByDate) Less(i, j int) bool {
    return s.Daters[i].GetDate() < s.Daters[j].GetDate()
}   

func main() {
    // lista and listb are just examples. They really come from elsewhere
    lista := []A{{"2012/08/01", "one"}, {"2012/08/03", "three"}}
    listb := []B{{"2012/08/02", 2}, {"2012/08/04", 4}}

    x := make([]Dater, len(lista) + len(listb))
    index := 0
    for i := range(lista) {
        x[index] = &lista[i]
        index++
    }
    for i := range(listb) {
        x[index] = &listb[i]
        index++
    }
    sort.Sort(ByDate{x})
    for _,v := range(x) {
        fmt.Printf("%#v\n", v)
    }
}

这样可行! 所以界面的基本用法很好,我开始理解界面好一点 - 谢谢!

注意:x的创建非常难看。 我看不到更清洁/更惯用的方式?

使用方法定义接口(比如Dated)(比如返回Date的getDate())。 然后让所有结构(A,B,C)实现Dated接口。 然后你可以定义use [] Dated来保存你的类型值。

您可能需要检查包'time'和'sort'以简化实现。

您可以使用嵌入

您将定义一个只包含日期的结构,然后将其嵌入其他结构中。 这样,您只需实现一次GetDate()。 您也可以随时扩展Date结构,而无需修改其他结构。

package main

type Dater interface {
    GetDate() string
}

type Date struct {
    Date string
}

func (d *Date) GetDate() string {
    return d.Date
}

type A struct {
    Date
    Info string
}

type B struct {
    Date
    Info []byte
}

type C struct {
    Date
    Info int32
}

您现在可以在A,B和C上调用GetDate()。

暂无
暂无

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

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