在golang中动态创建struct数组

在golang中动态创建struct数组,go,Go,我尝试创建一个接受任何结构值的泛型函数,并创建该结构类型的数组。这是我试过的代码。但我得到错误“t不是一个类型”。我如何实现这一点 type RegAppDB struct { nm string data []interface{} } func CreateRegTable(tbl string, rec interface{}) RegAppDB { t := reflect.TypeOf(rec) fmt.Println(t) ret

我尝试创建一个接受任何结构值的泛型函数,并创建该结构类型的数组。这是我试过的代码。但我得到错误“t不是一个类型”。我如何实现这一点

    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不支持泛型,任何这样做的尝试都不会成功。在您的具体案例中,有两个关键问题:

  • 不能将变量用作类型。Go是编译时静态类型化的,因此任何在运行时获取类型信息的操作(例如,
    reflect.TypeOf
    )都太晚了,无法使用您尝试的方式
  • 同样重要的是,结构的字段类型为
    []接口{}
    ,这意味着该字段只能使用
    []接口{}
    <例如,代码>[]字符串是另一种类型,不能分配给该字段

    • 我走了另一条路。需要一些美化。但这是可行的。所以现在数据是一个接口数组,我通过调用函数传递指向结构变量的指针来保存函数

          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)
      }
      
      类型RegAppDB struct{
      纳米线
      数据[]接口{}
      cnt-int
      }
      //CreateRegTable创建一个数据结构来保存回归结果
      func CreateRegTable(tbl字符串)*RegAppDB{
      return&RegAppDB{tbl,make([]接口{},20),0}
      }
      //Save实现保存记录回归应用程序数据库
      func(rga*RegAppDB)保存(rec接口{})错误{
      rga.data[rga.cnt]=rec
      rga.cnt++
      归零
      }
      //显示显示回归表的工具
      func(rga*RegAppDB)Show()错误{
      fmt.Println(rga.cnt)
      
      对于i:=0;i不能对泛型类型执行此操作。如果存在固定数量的可能类型,则可以执行以下操作:

      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
      }
      

      注意:
      RegAppDB
      中的
      数据应为
      接口{}
      类型,因为
      []int
      实现了
      接口{}
      而不是
      []接口{}

      不,我需要一个接口数组。我需要的数据是保存一个结构值数组。CreateRegTable创建一个结构并返回它。然后还有另一个函数将添加到数据数组中。发布了下面实现的完整代码。RegAppDB包含一个元素数据,其数据类型在编译时未知,因此其声明为s[]接口{}。我想让函数CreateRegTable做的是使用函数参数rec的运行时类型创建一个RegAppDB变量,并将字段数据作为运行时类型rec的数组。我从注释中了解到,这在go中是不可能的?