jjzjj

reflecting

全部标签

go - 如何在 reflect.New 创建时按其类型转换 reflect.Value

我实际上是在尝试在golang中使用反射的黑魔法:P我得到了这样的东西:var_intintvar_int32int32var_int64int64var_stringstringvarnilablesIndexmap[int]reflect.Valuevarnilables=map[string]reflect.Type{"int32":reflect.TypeOf(_int32)},"int64":reflect.TypeOf(_int64)},"int":reflect.TypeOf(_int)},"string":reflect.TypeOf(_string)},}nilabl

go - 是否可以通过Golang中的枚举类型反射(reflect)出枚举的变量

例如,我有一个像这样的枚举:typeStatusintconst(NormalStatus=itoaBlocked)Status的类型已知,我可以通过Status获取"Normal"和"Blocked"。如果有一种方法,我想就是通过reflect。我不想用映射来解决它。因为这个函数是在一个库中使用的。它是一种无状态的。还有其他方法吗?谢谢 最佳答案 常量名称不会在reflectAPI中公开。使用stringer工具。 关于go-是否可以通过Golang中的枚举类型反射(reflect)出

go - 从 reflect.Type 中删除指针

我有一个reflect.Type,它包含一个指向结构的双指针。我希望能够删除一个间接级别以获得指向该结构的指针。这可能吗?例如,我想这样做:funcfoo(xinterface{}){typ:=reflect.TypeOf(x)fmt.Printf("%v",typ)//prints**FoorealType:=typ.PointsTo()fmt.Printf("%v",typ)//prints*Foo}但据我所知,这个功能并不存在。有一个Indirect函数在Value上运行,但我看不到任何类似的适用于Type的函数。 最佳答案

go - 在go中,为什么打印出来的reflected value和它的interface一样?

摘自LawsofReflection:(Whynotfmt.Println(v)?Becausevisareflect.Value;wewanttheconcretevalueitholds.)这让我很困惑,因为下面的代码:varxfloat64=3.4varv=reflect.ValueOf(x)fmt.Println("valueofxis:",v)y:=v.Interface().(float64)//ywillhavetypefloat64.fmt.Println("interfaceofvalueofxis:",y)打印相同的输出:valueofxis:3.4interfac

pointers - reflect.Pointer() 返回值不是值地址

typeBookInfostruct{Meta*TableMeta...}func(si*schemaInfo)getTabInfo(objinterface{})(*tabInfo,error){typ:=reflect.TypeOf(obj)val:=reflect.ValueOf(obj)iftyp.Kind()!=reflect.Ptr{returnnil,errors.New("nborm.schemaInfo.getDBInfo()error:requiredapointer")}meta:=*(**TableMeta)(unsafe.Pointer(val.Pointer

reflection - 在 go 反射包中,调用 Value.Kind() 是 Value.Type().Kind() 的语法糖吗?

两个reflect.Type接口(interface)和reflect.Valuetype实现相同的Kind()方法签名,假设我们有一些值对象v:=reflect.ValueOf(x)v.Kind()只是调用v.Type().Kind()吗? 最佳答案 它们包含相同的值,但似乎指的不是同一件事:type.go来源value.go来源Type通常由未导出的结构rtype实现(通过TypeOf),而Value包含一个*rtype并扩展flag,它本身是Kind的简化形式://flagholdsmetadataaboutthevalue.

go - 使用 reflect 设置 struct of struct values 的值

我有一些看起来可以工作但最终什么也没做的代码:http://play.golang.org/p/TfAWWy4-R8有一个结构,该结构具有结构类型的字段。内部结构具有所有字符串字段。在循环中使用反射,想要从外部结构中获取所有结构字段。接下来,填充内部结构中的所有字符串值。示例代码从标签中获取文本并在“,”上对其进行解析,以获取内部循环的字符串值。这是应该创建内部结构并将解析的数据添加到字符串值的主要部分。t:=reflect.TypeOf(Alias{})alias=reflect.New(t)fori:=0;i当您查看示例的输出时,它看起来像是在工作,但是在从外部结构打印一个值之后,

reflection - Go 反射(reflect)字段索引 - 单个索引与 slice

reflect.StructField有一个类型为[]int的Index字段。关于此的文档有点令人困惑:Index[]int//indexsequenceforType.FieldByIndex当然Type.FieldByIndex也符合预期,对其行为有更清晰的解释://FieldByIndexreturnsthenestedfieldcorresponding//totheindexsequence.ItisequivalenttocallingField//successivelyforeachindexi.//Itpanicsifthetype'sKindisnotStruct.

reflection - 是否可以使用反射来做类似于类型切换的事情?

我需要根据反射(reflect)的值(value)类型做不同的事情。value:=reflect.ValueOf(someInterface)我想做一些具有以下效果的事情:if=={dosomething}elseif=={dosomething}这类似于go代码中的类型切换。 最佳答案 如果您正在迭代结构的字段,您可以使用类型开关根据字段的类型执行不同的操作:value:=reflect.ValueOf(s)fori:=0;ihttps://play.golang.org/p/-B3PWMqWTo

reflection - 查找对象支持的所有导入接口(interface)

我有一个类似os.Stdout的对象,我想知道它是否支持我平台上的io.WriteCloser。我可以获得我的对象的类型,但它没有告诉我任何关于接口(interface)的信息。packagemainimport("fmt";"reflect";"os")funcmain(){fmt.Println(reflect.TypeOf(os.Stdout))}此代码将*os.File打印到控制台。如果os.File匹配io.WriteCloser方法,我可以手动查找,但我很想知道该对象支持的所有接口(interface)。 最佳答案 这不