Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add elements to slice reflection?

Hi i am learning Go and i was doing some reflection. I got stuck in a case like this:

  1. I want to create a slice of struct passed to function as interface{}
  2. Then I want to append new elements to this slice

Here is a playground with the code example.

package main

import (
    "fmt"
    "reflect"
)

type A struct{ Name string }

func main() {
    bbb(A{})
}

func aaa(v interface{}) {
    sl := reflect.ValueOf(v).Elem()
    typeOfT := sl.Type()

    ptr := reflect.New(typeOfT).Interface()
    s := reflect.ValueOf(ptr).Elem()
    sl.Set(reflect.Append(sl, s))

    ptr = reflect.New(typeOfT).Interface()
    s = reflect.ValueOf(ptr).Elem()
    sl.Set(reflect.Append(sl, s))
}

func bbb(v interface{}) {
    myType := reflect.TypeOf(v)
    models := reflect.Zero(reflect.SliceOf(myType)).Interface()
    aaa(&models)

    fmt.Println(models)
}

Error: panic: reflect: call of reflect.Append on interface Value

Is there a way to make it work ? Note: that i want to work on a reference.

Solution:

Here is what i manage to do: playground.

like image 828
vardius Avatar asked Jun 21 '16 08:06

vardius


1 Answers

The problem was with reflect.Zero when i created slie with reflect.New it worked. Here you have full working example.

https://play.golang.org/p/3mIEFqMxk-

package main

import (
    "encoding/json"
    "fmt"
    "reflect"
)

type A struct {
    Name string `column:"email"`
}

func main() {
    bbb(&A{})
}

func aaa(v interface{}) {
    t := reflect.TypeOf(v)
    if t.Kind() == reflect.Ptr {
        t = t.Elem()
    }

    if t.Kind() == reflect.Slice {
        t = t.Elem()
    } else {
        panic("Input param is not a slice")
    }

    sl := reflect.ValueOf(v)

    if t.Kind() == reflect.Ptr {
        sl = sl.Elem()
    }

    st := sl.Type()
    fmt.Printf("Slice Type %s:\n", st)

    sliceType := st.Elem()
    if sliceType.Kind() == reflect.Ptr {
        sliceType = sliceType.Elem()
    }
    fmt.Printf("Slice Elem Type %v:\n", sliceType)

    for i := 0; i < 5; i++ {
        newitem := reflect.New(sliceType)
        newitem.Elem().FieldByName("Name").SetString(fmt.Sprintf("Grzes %d", i))

        s := newitem.Elem()
        for i := 0; i < s.NumField(); i++ {
            col := s.Type().Field(i).Tag.Get("column")
            fmt.Println(col, s.Field(i).Addr().Interface())
        }

        sl.Set(reflect.Append(sl, newitem))
    }
}

func bbb(v interface{}) {
    t := reflect.TypeOf(v)
    if t.Kind() == reflect.Ptr {
        t = t.Elem()
    }
    if t.Kind() != reflect.Struct {
        panic("Input param is not a struct")
    }

    models := reflect.New(reflect.SliceOf(reflect.TypeOf(v))).Interface()

    aaa(models)

    fmt.Println(models)

    b, err := json.Marshal(models)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(b))
}
like image 87
vardius Avatar answered Sep 27 '22 18:09

vardius