- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
调用反射值的 .FieldByName 方法时出现以下错误,确切的错误是:-
panic: reflect: call of reflect.Value.FieldByName on ptr Value
代码是:-
s := reflect.ValueOf(&value).Elem() (value is a struct)
metric := s.FieldByName(subval.Metric).Interface() (subval.Metric is a string)
我知道这并不多,但这是我能得到的所有信息。
这是 Go Playground 上代码的链接:http://play.golang.org/p/E038cPOoGp
最佳答案
你的 value
已经是一个指向结构的指针。尝试在您的代码中打印出 s.Kind()
。
没有理由获取 value
的地址,然后在该 reflect.Value
上调用 Elem()
,这会取消引用您的指针刚刚创建。
s := reflect.ValueOf(value).Elem()
metric := s.FieldByName(subvalMetric).Interface()
fmt.Println(metric)
关于reflection - Reflect.Value.FieldByName 导致 panic ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24537525/
我读过一篇博客文章(目前我找不到链接),其中作者说分配局部变量比使用 ADOQuery.FieldByName('...').asString 或更快TSQLQuery.FieldByName('..
我碰巧有一个非常有趣的查询和问题。我们正在运行 Oracle 12c(大约一年前从 11g 迁移),最近刚刚从 jOOq 3.4.x 迁移到 3.7.3,我不想使用现在已弃用的 DSL.fieldBy
我正在使用Delphi XE2。我的代码从 SQL-Server 2008 R2 数据库中提取数据。返回的数据是一个包含 1,055,227 字节数据的 nvarchar(max) 字段。我使用以下代
我说的是 Delphi + ADO + MSSQL。 好的,我知道带有参数的查询对于 SQL 注入(inject)是非常安全的。 另一方面,动态查询是相当不安全的。 但是经典的 FieldByName
在我维护的一些代码中,我看到 TClientDataSet.OnCalcFields 事件处理程序中使用了两种不同的方法: with DataSet do begin // 1. Call
我正在尝试使用 golang 中的反射从一个结构中读取,我能够成功地做到这一点,但我想知道我该怎么做才能忽略字段名称的大小写。 我有下面的代码 type App struct{ AppID
我将数据传递给具有输入类型接口(interface)的函数。 这段代码: main(){ SampleData := Input{ Recipients: []string{"abc"
在下面的示例中,当未找到 field 时,我正在尝试找出如何停止执行我的程序。 如果 FieldByName(key) 返回零值,我该如何警告用户未找到该字段? field := mutable.Fi
调用反射值的 .FieldByName 方法时出现以下错误,确切的错误是:- panic: reflect: call of reflect.Value.FieldByName on ptr Valu
val 有什么值(value)? ,为字符串类型,当列名fieldName时获取是 null对于选定的行?这里myQry是一个数据库查询。 val := myQry.FieldByName('fiel
我有一个 project 函数,它返回一个 slice ,其中包含输入 slice 中每个结构或映射的名称的字段值。我遇到输入 slice 包含指向结构的指针的情况。我已经设置了一个递归函数来对值进行
我有一个类似这个演示的数据结构。 type Family struct { first string last string } type Person struct { name
我有一个类型为 interface{} 的变量,我想使用反射更改字段的值。我该怎么做?由于其他要求,变量必须是 interface{} 类型。如果变量不是 interface{} 类型,则一切正常,否
我是一名优秀的程序员,十分优秀!