[英]what's the difference between `reflect.ValueOf(&x).Elem` and `reflect.ValueOf(x)` in go?
[英]In Go, what's the inverse of reflect.SliceOf()?
在Go中, reflect.SliceOf()
使一个Type表示给定Type的切片:
SliceOf返回元素类型为t的切片类型。 例如,如果t表示int,则SliceOf(t)表示[] int。
但是,我已经有一个Type for []int
但是想要获得一个Type for int
。 有一个简单的方法吗? (注意,我使用int
作为示例。实际上,我所知道的是我有一个切片,我需要找到切片的每个元素的类型。)
我正在尝试使用反射从[]string
填充bool,int,float或string的切片...这是相关的部分:
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
}
}
但是为了填写切片,我需要知道我填写的切片的类型...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.