jjzjj

reflecting

全部标签

reflection - 将 reflect.Value 从字符串转换为 int

我遇到这样的错误reflect.Value.Convert:valueoftypestringcannotbeconvertedtotypeintgoroutine6当我运行这段代码时param:="1"//typestringps:=fn.In(i)//typeintif!reflect.TypeOf(param).ConvertibleTo(ps){fmt.Print("Couldnotconvertparameter.\n")//thisisprinted}convertedParam:=reflect.ValueOf(param).Convert(ps)我能否以某种方式做到这一

go - 反射(reflect)值(value)问题

在尝试测试此业务功能时://IsInSliceworkslikeArray.prototype.findinJavaScript,exceptit//returns-1if`value`isnotfound.(Also,Array.prototype.findtakes//function,andIsInSlicetakes`value`and`list`)funcIsInSlice(valueinterface{},listinterface{})int{slice:=reflect.ValueOf(list)fori:=0;i我发现它没有通过我的合理性测试:funcTestIsIn

go - 在struct方法中使用reflect调用struct的方法

关闭。这个问题是notreproducibleorwascausedbytypos.它目前不接受答案。这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这个问题的解决方式不太可能帮助future的读者。关闭4年前。Improvethisquestion如何在结构方法中使用反射来调用结构方法?例如:packagemainimport"fmt"import"reflect"typeTstruct{}func(t*T)New(){value:=getDynamicValue()method:=reflect.ValueOf(&t).MethodByNa

go - 反射(reflect)类型比较

我想确保map键的类型是string。Key()方法返回Type,我不确定检查它是否为string的正确方法是什么。我唯一想到的是:ifv.Type().Key()==reflect.TypeOf(""){fmt.Print("Itisstring")}这是正确的方法吗? 最佳答案 是的,如果键类型“完全”是string,您所做的报告。但例如,如果键类型是一个自定义类型,将string作为其基础类型,如本例所示:typemystrstringm:=map[mystr]int{}那么键类型将不等于reflect.TypeOf("")。

Golang : reflect. DeepEqual 返回 false

我很好奇为什么这个DeepEqual检查是错误的:packagemainimport("encoding/json""fmt""log""reflect""strings")typeResultstruct{Topicstring`json:"topic,omitempty"`Idint`json:"id,omitempty"`}//Resultrepresentsthereturnedcollectionfromatopicsearch.typeResultResponsestruct{Result[]Result`json:"results"`}funcmain(){want:=R

reflection - 错误的反射结构类型

我有2个不同的包(pkg1、pkg2),首先我有从另一个包调用函数的代码文件#1packagepkg1import"pkg2"import"reflect"typeUserstruct{namestring...}funcmain(){fmt.Println(reflect.TypeOf((*User)(nil))//=>*Userpkg2.RegisterStruct(reflect.TypeOf((*User)(nil))//pkg2.RegisterStruct(reflect.TypeOf(&User{})//alsotriedthisway}文件#2packagepkg2im

reflection - 如何设置结构字段抛出接口(interface)?

大家好!我的任务是解析命令行参数并填充结构字段。我的函数必须适用于所有类型的参数-它们将在struct标记中进行描述。例如:typeCommndLineArgumentsstruct{Configfilestring`required:"false"name:"config"default:"/etc/daemon.conf"description:"Configfile"`Daemonbool`required:"true"name:"daemon"default:"false"description:"Runasdaemon"`}我使用reflect和flag包。像这样:funcG

reflection - 使用反射包比较指针值

我有一个包含很多字段的结构,我必须检查这些字段中是否有任何字段为空,而不必手动键入每个字段名称。该字段的类型始终是一个指针,因此我可以进行检查而不必担心零值。我正在尝试使用反射包来解决这个问题,但它似乎无法正常工作,我也不知道为什么。这是一个复制我的问题的Playground:http://play.golang.org/p/LOb6a8eklE如您所见,如果我手动检查一切正常。当被要求打印时,它也打印null,但在比较时它的计算结果为false。对正在发生的事情有什么想法吗?我的主要猜测是因为Interface()的返回类型显然是interface{},并且通过在其中存储“null”

reflection - 戈朗 : How can I use refect package with exsisting library

我想从函数名调用现有库中的函数。在golang中,只要从methodname调用method就OK了,因为reflectpackage有(vValue)MethodByName(namestring)。但是,对于调用方法,所有方法参数都应该是reflect.Value。如何调用参数不是reflect.Value的函数。packagemain//-------------------------------//Exampleofexistinglibrary//-------------------------------typeClientstruct{idstring}typeMet

dictionary - 反射(reflect)一片map,每张map都是struct类型?

我正在尝试遍历interfacedialogCommands,它是一个slice。我可以正常地遍历它,每个Index中的Println都会给我一个map。但是,此map被打印为具有类型structifreflect.TypeOf(dialogCommands).Kind()==reflect.Slice{commands:=reflect.ValueOf(dialogCommands)fori:=0;i输出结果是structmap[options:[abc]]structmap[startDialogs:[dialog1]]如您所见,类型是struct,但输出是map。如何遍历v的ke