简体   繁体   English

在golang中动态创建结构数组

[英]Creating array of struct dynamically in golang

I try to create a generic function that accepts any struct value and create a array of that struct type.我尝试创建一个通用函数,该函数接受任何结构值并创建该结构类型的数组。 Here is the code I tried.这是我试过的代码。 But I get the error "t is not a type".但我收到错误“t 不是类型”。 How can I implement this.我该如何实现这一点。

    type RegAppDB struct {
    nm   string
    data []interface{}
}

func CreateRegTable(tbl string, rec interface{}) RegAppDB {
    t := reflect.TypeOf(rec)
    fmt.Println(t)
    return RegAppDB{"log", []t}
}

Go does not support generics, and any attempt to do something like that is not going to work out well. Go 不支持泛型,任何类似的尝试都不会奏效。 In your specific case, there are a couple of key problems:在您的具体情况下,有几个关键问题:

  • You cannot use a variable as a type.您不能将变量用作类型。 Go is compile-time static typed, so anything that gets type information at runtime (ie reflect.TypeOf ) happens too late to use the way you're trying to do it. Go 是编译时静态类型的,因此在运行时获取类型信息的任何内容(即reflect.TypeOf )都太晚了,无法使用您尝试使用的方式。
  • Equally important, your struct's field is of type []interface{} , which means the only type you can use for that field is []interface{} .同样重要的是,您的结构的字段是[]interface{}类型,这意味着您可以用于该字段的唯一类型[]interface{} []string , for example, is a different type, and cannot be assigned to that field.例如, []string是一种不同的类型,不能分配给该字段。

I took another route.我走了另一条路。 Need some beautification.需要一些美化。 But this works.但这有效。 So now data is an array of interface and from calling function I pass pointer to structure variables to Save function.所以现在数据是一个接口数组,从调用函数我将指向结构变量的指针传递给 Save 函数。

    type RegAppDB struct {
    nm   string
    data []interface{}
    cnt  int
}

// CreateRegTable creates a data structure to hold the regression result
func CreateRegTable(tbl string) *RegAppDB {
    return &RegAppDB{tbl, make([]interface{}, 20), 0}
}

// Save implements saving a record Regression application DB
func (rga *RegAppDB) Save(rec interface{}) error {
    rga.data[rga.cnt] = rec
    rga.cnt++
    return nil
}

// Show implements showing the regression table
func (rga *RegAppDB) Show() error {
    fmt.Println(rga.cnt)
    for i := 0; i <= rga.cnt; i++ {
        fmt.Println(rga.data[i])
    }
    return nil
}

// Compare compares two regression table for equality
func (rga *RegAppDB) Compare(rgt *RegAppDB) bool {
    return reflect.DeepEqual(rga, rgt)
}

It cannot be done for a generic type.它不能用于泛型类型。 If there are a fixed number of possible types, then you can do something like the following:如果有固定数量的可能类型,那么您可以执行以下操作:

type RegAppDB struct {
    nm   string
    data interface{}
}

func CreateRegTable(rec interface{}) RegAppDB {
    switch rec.(type) {
    case int:
        return &RegAppDB{"log", []int{}}
    case string:
        return &RegAppDB{"log", []string{}}
    }
    return nil
}

Note: Your data in RegAppDB should be of type interface{} since []int implements interface{} but not []interface{}注意:您在RegAppDB data应该是interface{}类型,因为[]int实现了interface{}而不是[]interface{}

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

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