In Go, reflect.SliceOf()
makes a Type representing a slice of the given Type:
SliceOf returns the slice type with element type t. For example, if t represents int, SliceOf(t) represents []int.
However, I already have a Type for []int
but want to get a Type for int
. Is there an easy way to do that? (Note, I'm using int
as an example. In reality, all I know is I have a slice, and I need to find what Type each element of the slice is.)
I'm trying to populate a slice of bool, int, float, or string, from a []string
using reflection... here's the relevant piece:
numElems := len(req.Form["keyName"])
if structField.Kind() == reflect.Slice && numElems > 0 {
slice := reflect.MakeSlice(structField.Type(), numElems, numElems)
for i := 0; i < numElems; i++ {
// I have some other code here to fill out the slice
}
}
But in order to fill out the slice, I need to know the type of the slice I'm filling out...