草庐IT

Reflection

全部标签

go - 反射(reflect)接口(interface)列表

我已经阅读了几个关于Go中反射的示例/问题,但我仍然无法理解我应该如何处理我的接口(interface)列表。以下是真实用例的精简版。我有几种符合给定接口(interface)的类型:typeFoointerface{Value()int}typebarstruct{valueint}func(bbar)Value()int{returnb.value}typebazstruct{}func(bbaz)Value()int{return42}我有一个这样的人的名单typeFoos[]Foovarfoos=Foos{bar{},baz{},}我想通过更改具有value字段的成员的值来遍历

go - 反射(reflect)接口(interface)列表

我已经阅读了几个关于Go中反射的示例/问题,但我仍然无法理解我应该如何处理我的接口(interface)列表。以下是真实用例的精简版。我有几种符合给定接口(interface)的类型:typeFoointerface{Value()int}typebarstruct{valueint}func(bbar)Value()int{returnb.value}typebazstruct{}func(bbaz)Value()int{return42}我有一个这样的人的名单typeFoos[]Foovarfoos=Foos{bar{},baz{},}我想通过更改具有value字段的成员的值来遍历

go - 反射以测试值是否为字符串

我正在尝试以最快和最简单的方式测试来自gjson库的值是否为字符串。我不想使用开关类型断言。ifreflect.TypeOf(gjson.Get(input,"name").Value())!="string"{return"Notastring!"}我的代码有什么问题? 最佳答案 gjson.Get返回Result,所以你可以简单地检查它的Type字段:ifgjson.Get(input,"name").Type!=gjson.String{return"Notastring!"} 关

go - 反射以测试值是否为字符串

我正在尝试以最快和最简单的方式测试来自gjson库的值是否为字符串。我不想使用开关类型断言。ifreflect.TypeOf(gjson.Get(input,"name").Value())!="string"{return"Notastring!"}我的代码有什么问题? 最佳答案 gjson.Get返回Result,所以你可以简单地检查它的Type字段:ifgjson.Get(input,"name").Type!=gjson.String{return"Notastring!"} 关

reflection - 在方法调用中找到真正的调用者

我有一个这样的结构:typeParentstruct{examplestring}func(p*Parent)GetMyAttr(){typ:=reflect.TypeOf(p).Elem()fori:=0;i如果我有另一个这样的结构:typeChildstruct{Parentanotherstring}像这样在child中调用GetTypeOfMe()ch:=Child{Parent{"example"},"another"}ch.GetMyAttr()总是返回example:string。是否可以通过反射在父结构中获取子结构?完整代码在这里http://play.golang.o

reflection - 在方法调用中找到真正的调用者

我有一个这样的结构:typeParentstruct{examplestring}func(p*Parent)GetMyAttr(){typ:=reflect.TypeOf(p).Elem()fori:=0;i如果我有另一个这样的结构:typeChildstruct{Parentanotherstring}像这样在child中调用GetTypeOfMe()ch:=Child{Parent{"example"},"another"}ch.GetMyAttr()总是返回example:string。是否可以通过反射在父结构中获取子结构?完整代码在这里http://play.golang.o

go - 使用反射附加到 go lang slice

出于某种原因,使用反射向slice添加新元素似乎不会更新slice本身。这是要演示的代码:packagemainimport("fmt""reflect")funcappendToSlice(arrPtrinterface{}){valuePtr:=reflect.ValueOf(arrPtr)value:=valuePtr.Elem()value=reflect.Append(value,reflect.ValueOf(55))fmt.Println(value.Len())//prints1}funcmain(){arr:=[]int{}appendToSlice(&arr)fmt

go - 使用反射附加到 go lang slice

出于某种原因,使用反射向slice添加新元素似乎不会更新slice本身。这是要演示的代码:packagemainimport("fmt""reflect")funcappendToSlice(arrPtrinterface{}){valuePtr:=reflect.ValueOf(arrPtr)value:=valuePtr.Elem()value=reflect.Append(value,reflect.ValueOf(55))fmt.Println(value.Len())//prints1}funcmain(){arr:=[]int{}appendToSlice(&arr)fmt

api - 避免在包 API 中暴露反射

在AlanDonovan和BrianKernighan的“TheGoprogramminglanguage”一书p333(第12.3节Display,递归值打印机)中提到Wherepossible,youshouldavoidexposingreflectionintheAPIofapackage.We'lldefineanunexportedfunctiondisplaytodotherealworkoftherecursion,andexportDisplay,asimplewrapperarounditthatacceptsaninterface{}parameter.funcD

api - 避免在包 API 中暴露反射

在AlanDonovan和BrianKernighan的“TheGoprogramminglanguage”一书p333(第12.3节Display,递归值打印机)中提到Wherepossible,youshouldavoidexposingreflectionintheAPIofapackage.We'lldefineanunexportedfunctiondisplaytodotherealworkoftherecursion,andexportDisplay,asimplewrapperarounditthatacceptsaninterface{}parameter.funcD