1

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". 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}
}
3
  • no I need an array of interfaces. What I need data is to hold an array of struct values. Commented Aug 28, 2018 at 16:14
  • CreateRegTable creates a struct and returns it. There is another function then would add into data array. Posted the full code implemented below. Commented Aug 28, 2018 at 16:20
  • RegAppDB contains an element data for which the data type is not known at compile time, so its declared as []interface{}. What I want function CreateRegTable to do is to use the run time type of function parameter rec to create a variable of RegAppDB with field data as an array of run time type of rec. What I undersatnd from comments is this is not possible in go? Commented Aug 28, 2018 at 17:46

3 Answers 3

4

Go does not support generics, and any attempt to do something like that is not going to work out well. 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 (i.e. reflect.TypeOf) happens too late to use the way you're trying to do it.
  • Equally important, your struct's field is of type []interface{}, which means the only type you can use for that field is []interface{}. []string, for example, is a different type, and cannot be assigned to that field.
Sign up to request clarification or add additional context in comments.

Comments

2

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.

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.