如何投射reflect.Value到它的类型? [英] How to cast reflect.Value to its type?

查看:329
本文介绍了如何投射reflect.Value到它的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将reflect.Value转换为其类型?

 类型Cat结构{
年龄int


cat:= reflect.ValueOf(obj)
fmt.Println(cat.Type())// Cat

fmt.Println(Cat cat).Age)//不会编译
fmt.Println((cat。(Cat))。Age)// same

谢谢!

解决方案

好的,我发现它

reflect.Value 有一个函数 Interface(),它将它转换为 interface {}


How to cast reflect.Value to its type?

type Cat struct { 
    Age int
}

cat := reflect.ValueOf(obj)
fmt.Println(cat.Type()) // Cat

fmt.Println(Cat(cat).Age) // doesn't compile
fmt.Println((cat.(Cat)).Age) // same

Thanks!

解决方案

Ok, I found it

reflect.Value has a function Interface() that converts it to interface{}

这篇关于如何投射reflect.Value到它的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆