I have a function that takes a struct type and attempts to pass in a reflect.Type
and later get the address of a slice created with the reflect
package but I can't seem to get the right type for the final object.
The hardcoded version looks like this:
val := new(StructA)
valSlice := make([]*StructA, desiredSize)
foo(&valSlice)
and the reflect
version I have looks like this:
inputType := reflect.TypeOf(&StructA{}) // This is a requirement
structType := inputType.Elem()
val := reflect.New(structType).Interface()
sliceType := reflect.SliceOf(inputType)
structSlice := reflect.MakeSlice(sliceType, desiredSize, desiredSize)
valSlice := structSlice.Interface()
This appears to work until I attempted to take the address of the two values:
foo(&val)
The hardcoded gives me this &[<nil> <nil> <nil> <nil> <nil>]
with type *[]*main.StructA
and the generated gives me this 0xc00010a190
with type *interface {}
.
Here is a repro of the issue with print statements littering for inspection: https://play.golang.org/p/ItKWS-PndTE
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire