草庐IT

reflectable

全部标签

reflection - Golang 对象的静态与动态绑定(bind)

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭7年前。Improvethisquestion我有这个应用程序,其中根据结构中的字符串过滤请求并执行不同的功能。我的方法是使用一个Map将字符串映射到函数指针并执行它们。然而,这种方法正在被一个想通过反射进行过滤的队友所争辩。我们正在使用Go,它用于监控我们网站的事件。队友做法:使用反射根据字符串切换对象,将字符串传给函数让函数调用相关函数。我的方法:从字符串到函数的简单映射感谢任何帮助。

reflection - 反射(reflect) : How to get the name of a struct field?

typeUserstruct{Namestring}functest(ointerface{}){t:=reflect.TypeOf(o)fmt.Println(t)}u:=&User{"Bob"}test(u.Name)//prints"string",butIneed"Name"这在Go中可能吗?我希望拥有尽可能少的“魔术弦”,所以不要UpdateFields("姓名","密码")我更愿意使用UpdateFields(user.Name,user.Password) 最佳答案 你不能那样做。我能想到的最接近的东西,但它太丑了所以

go - reflect.TypeOf 的反转

我想取回我曾经保存过的值的类型。我使用了reflect.Typeof()并保存了类型。然后尝试使用开关类型。类型将始终为“*reflect.rtype”。我无法通过类型断言检索任何一个。packagemainimport("fmt""reflect")funcmain(){varalltypes[]interface{}alltypes=append(alltypes,reflect.TypeOf(true))alltypes=append(alltypes,reflect.TypeOf(0.0))alltypes=append(alltypes,reflect.TypeOf(0))f

reflection - 不能在用反射制作的 slice 上使用范围,然后传递 json.Unmarshal

我从下面的代码中得到以下错误:invalidindirectoftypedSlice(typeinterface{})cannotrangeovertypedSlice(typeinterface{})这让我感到困惑,因为reflect.TypeOf(copy)匹配t的类型。funcUnmarshal(treflect.Type)[]interface{}{ret:=[]interface{}{}s:=`[{"Name":"Thequick..."}]`slice:=reflect.Zero(reflect.SliceOf(t))o:=reflect.New(slice.Type())

reflection - Go Reflect 方法调用无效的内存地址或 nil 指针取消引用

我正在尝试使用反射来调用结构上的方法。但是,即使attachMethodValue和args都不是-零。关于它可能是什么的任何想法?去Playground:http://play.golang.org/p/QSVTSkNKampackagemainimport"fmt"import"reflect"typeUserControllerstruct{UserModel*UserModel}typeUserModelstruct{Model}typeModelstruct{transactionService*TransactionService}func(m*Model)Attach(t

testing - 测试 '-timeout 0' 未反射(reflect)在执行中

我正在使用Go1.9.2版本并使用-timeout0标志调用禁用超时的测试。gotestmy_module-runTestModule-v--race-timeout0但是测试执行在默认超时10m后超时。***Testkilled:rantoolong(10m0s). 最佳答案 它是test.timeout即gotestmy_module-runTestModule-v--race-test.timeout0 关于testing-测试'-timeout0'未反射(reflect)在执行中

reflection - 使类型指向带有反射的指针

鉴于此:varvreflect.Value=...v.Type()//*model.Company如何实例化一个新的model.Company并通过反射修改它的字段? 最佳答案 类似的东西:v:=reflect.ValueOf(&Company{})t:=v.Type()c:=reflect.New(t.Elem()).Elem()c.FieldByName("Name").SetString("ReflectionInc.")fmt.Printf("%#v\n",c.Interface())//=>main.Company{Nam

go - 如何使用 reflect 包访问函数的返回数据?

我有一个IndexController类型的函数:func(thisIndexController)ActionIndex()map[string]string{returnmap[string]string{"Name":"HellofromtheactionIndex()!"}}它是这样使用的:routerInstance:=router.Constructor(request)controllerObject:=controllers[routerInstance.GetRequestController(true)]outputData:=reflect.ValueOf(con

reflection - 将 slice 复制到反射数组中

我有一个看起来像这样的结构:typeRecordstruct{NamestringQuestionType[2]byte//ArraymaybearbitrarylengthClass[3]byte}我试图用bytes.Buffer中的字节填充结构(由于字节数据中的一些额外复杂性,我无法使用binary.Read。)我正在使用reflect包迭代结构的元素,并从bytes.Buffer中读取到结构中。funcfillStructure(buffer*bytes.Buffer)*Record{//Thisishard-codednow,butwillbepassedinasaninter

go - 如何使用 Go reflect 向 cron 添加功能?

我正在尝试通过从如下文件中读取配置来动态安排一些作业import("github.com/robfig/cron""fmt")masterJobDetails:=//thisisarrayofjobfromfilec:=cron.New()fork,v:=rangemasterJobDetails{fmt.Println(k,v.JobName)c.AddFunc(v.CronExpression,v.JobName)//JobNameisfunctionnameinstringformatwhichneedtocallonspecificinterval}c.Start()c.Add