I need to create a slice of the structure from its reflection interface.
I used Reflection because I cannot see another solution without using it.
In short, a function gets the variable values ββof an interface.
Then, when the reflection creates a slice and passes it to another function.
Reflection asks for approval type
SliceVal.Interface().(SomeStructType)
But I canβt use it.
Code on the playground http://play.golang.org/p/EcQUfIlkTe
Code:
package main import ( "fmt" "reflect" ) type Model interface { Hi() } type Order struct { H string } func (o Order) Hi() { fmt.Println("hello") } func Full(m []Order) []Order{ o := append(m, Order{H:"Bonjour"} return o } func MakeSlices(models ...Model) { for _, m := range models { v := reflect.ValueOf(m) fmt.Println(v.Type()) sliceType := reflect.SliceOf(v.Type()) emptySlice := reflect.MakeSlice(sliceType, 1, 1) Full(emptySlice.Interface()) } } func main() { MakeSlices(Order{}) }
source share