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

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

IIS->Tomcat Redirect: multiple worker with default -