reflection - how to make array with given name(string) in golang with reflect -
i want make array name in golang, got error here code package main
import ( "fmt" "reflect" ) type struct{ name string id int } func main() { := &my{} mytype := reflect.typeof(my) fmt.println(mytype) //v := reflect.new(mytype).elem().interface() // want make array //a := make([](mytype.(type),0) //can compile //a := make([]v.(type),0) ////can compile fmt.println(a) }
i believe you're looking for:
slice := reflect.makeslice(reflect.sliceof(mytype), 0, 0).interface()
working example:
as side note, in cases nil slice more suitable 1 capacity zero. if want nil slice, instead:
slice := reflect.zero(reflect.sliceof(mytype)).interface()
Comments
Post a Comment