草庐IT

REFLECTION

全部标签

go - 接口(interface)指针的奇怪行为

我写了3个类似的函数来找出Go指针反射的一个奇怪行为。packagemainimport("reflect""fmt")variinterface{}=struct{}{}//iisaninterfacewhichpointstoastructvarptr*interface{}=&i//ptrisi'spointerfuncf(xinterface{}){//printx'sunderlyingvaluefmt.Println(reflect.ValueOf(x).Elem())}funcmain1(){//fisaskingforinterface?OK,I'llusethestr

go - 函数类型转换

如何将funcadd(a,bint)int转换为func(...interface{})interace{}类型?关于使用reflect实现通用函数的任何想法包? 最佳答案 正如JimB所说,您不能在Go中强制转换,也不能像那样转换函数,但是通过使用闭包,您可以快速包装您的函数:funcadd(a,bint)int{returna+b;}wrap:=func(args...interface{})interface{}{returninterface{}(add(args[0].(int),args[1].(int)))}请注意,如

go - Go 中的泛型编程,隐式泛型类型

我需要Go来隐式解析我的结构类型,以便对某些属性进行通用替换。//mustreplacetheattributewithattValuefuncSetAttribute(objectinterface{},attributeNamestring,attValueinterface{},objectTypereflect.Type)interface{}{///worksperfectly,butfunctionSetAttributeneedstoknowCustomertypetodotheconvertionconvertedObject:=object.(Customer)//请

reflection - 如何反射(reflect)Golang中对象的动态方法名

例子router.Get(path,handler)//worksfinemethodStr="Get"router.methodStr(path,handler)//errorfuncs:=map[string]func(){"methodStr":"Get"}router.funcs["methodStr"](path,handler)//errorreflect.ValueOf(router).MethodByName("Get").Call([]reflect.Value{})//error我正在获取字符串形式的方法名称。如何使用字符串名称调用路由器对象方法

go - 如何在 Go 中实例化未知类型的值?

我用golang开发了一些服务器。我尝试创建一些包装函数,这对我将来有帮助。我有什么:1)我有一些DTO结构,例如:typeRequeststruct{Field1string`json:"field1"`Field2string`json:"field2"`}typeResponsestruct{Field1string`json:"field1"`Field2string`json:"field2"`Field3string`json:"field3"`}2)我在Controller层有一些函数,它们(按照惯例)接收1个参数(指向结构的指针)并返回1个结果(指向结构的指针),例如:

golang,从另一个变量创建一个变量

我想:用另一个变量的类型创建一个变量。源变量是数值型的(int,int16,float32,...)对该变量进行一些简单的操作(+、-、...)。这段代码工作正常:packagemainimport("fmt""reflect")funcinit1(vinterface{})interface{}{switchreflect.ValueOf(v).Kind(){casereflect.Int:returnint(0)casereflect.Float32:returnfloat32(0)casereflect.Float64:returnfloat64(0)}returnnil}fun

go - 如何在 Go 中使用反射将结构的 nil 指针设置为结构的零值?

我正在递归地抓取结构。它与json包的作用相同。如果遇到指向结构的nil指针,则应将指针设置为结构的零值,以便能够继续挖掘。我怎样才能做到这一点?varunmarshalfunc(sreflect.Value)errorunmarshal=func(sreflect.Value)error{t:=s.Type()fori:=0;i 最佳答案 您可以使用reflect.New(f.Type.Elem())创建一个指向零值的指针,然后使用v.Set(value)来设置它。根据反射(reflect)文档:New返回一个值,表示指向指定类型

reflection - 在 Go 中获取空结构 slice 的字段

我有一个函数func(r*render)foo(vinterface{}){val:=reflect.ValueOf(v)fields:=structs.Fields(val.Index(0).Interface())...它接受一片结构并尝试获取v的字段,但是,如果v为空,则“val.Index(0)”会使程序崩溃。有更好的方法吗? 最佳答案 你需要先检查你是否有一个slice开始,然后检查你是否有一个空slice,你可能应该检查你是否也有一个结构:(example)val:=reflect.ValueOf(v)ifval.Kin

reflection - Go: reflect : 调用输入参数太少

我一直坚持使用反射库的问题。由于很多推荐,我决定使用它,但我只是在学习,有些部分并不是很容易..我有这部分代码:funccountDataByName(sourceNamestring,statDatainterface{},filtersFilter,chartNamestring)[]ChartElement{...//step1-filterfilteredData:=reflect.ValueOf(statData).MethodByName("FilterData").Call([]reflect.Value{})//step2-cluster//clusterData:=r

go - Go中如何使用reflect递归解析嵌套结构?

我有一个嵌套的三层结构。我想在Go中使用reflect来解析它(使用递归函数)。使用反射和递归函数的原因是可以有不同数量的字段(但前两个字段是固定的)字段类型不固定。嵌套层的数量可以不同(在这个例子中只有三层。它可以更多)这里是一些代码。typeEdgestruct{Uidstring`json:"uid,omitempty"`Namestring`json:"name,omitempty"`ReadArticle`json:"visited,omitempty"`}typeArticlestruct{Uidstring`json:"uid,omitempty"`Namestring`