如何解决有人可以解释这个结果吗?
有一个关于反射的演示:
func main() {
test := []string{"hello"}
V := reflect.ValueOf(test)
if reflect.TypeOf(V).Kind() == reflect.Struct{
fmt.Printf("it's a struct")
} else {
fmt.Printf("other")
}
}
,输出为"it's a struct"
解决方法
V
是reflect.ValueOf()
返回的值,所以它是类型为reflect.Value
的结构,因此reflect.TypeOf(V).Kind()
是reflect.Struct
。没什么奇怪的。
通过reflect
包:
type Value struct {
// contains filtered or unexported fields
}
如果您本身通过test
,则其种类为reflect.Slice
:
if reflect.TypeOf(test).Kind() == reflect.Slice {
fmt.Printf("it's a slice")
} else {
fmt.Printf("other")
}
这将打印(在Go Playground上尝试):
it's a slice
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。