我在调用反射值的.FieldByName方法时遇到以下错误,确切的错误是: -
panic: reflect: call of reflect.Value.FieldByName on ptr Value
Run Code Online (Sandbox Code Playgroud)
而代码是: -
s := reflect.ValueOf(&value).Elem() (value is a struct)
metric := s.FieldByName(subval.Metric).Interface() (subval.Metric is a string)
Run Code Online (Sandbox Code Playgroud)
我知道这并不多,但这是我能得到的所有信息.
这是Go Playground代码的链接:http://play.golang.org/p/E038cPOoGp
Jim*_*imB 13
您value已经是指向结构的指针.尝试s.Kind()在代码中打印出来.
没有理由获取地址value,然后调用Elem()它reflect.Value,取消引用刚刚创建的指针.
s := reflect.ValueOf(value).Elem()
metric := s.FieldByName(subvalMetric).Interface()
fmt.Println(metric)
Run Code Online (Sandbox Code Playgroud)