qwe*_*xcv 2 reflection go reflect
我有一个类型的变量,interface{}我想使用反射更改字段的值。我该怎么做?interface{}由于其他要求,变量必须属于类型。如果变量不是interface{}所有工作类型,否则代码抛出
reflect: call of reflect.Value.FieldByName on interface Value
Run Code Online (Sandbox Code Playgroud)
我的代码
package main
import (
"fmt"
"reflect"
)
func main() {
a := struct {
Name string
}{}
// works
reflect.ValueOf(&a).Elem().FieldByName("Name").SetString("Hello")
fmt.Printf("%#v\n", a)
var b interface{}
b = struct {
Name string
}{}
// panics
reflect.ValueOf(&b).Elem().FieldByName("Name").SetString("Hello")
fmt.Printf("%#v\n", b)
}
Run Code Online (Sandbox Code Playgroud)
应用程序必须调用Elem()两次才能获取结构值:
reflect.ValueOf(&b).Elem().Elem().FieldByName("Name").SetString("Hello")
Run Code Online (Sandbox Code Playgroud)
第一次调用Elem()取消引用指向 的指针interface{}。第二次调用Elem()获取接口中包含的值。
随着这种变化,恐慌是reflect.Value.SetString using unaddressable value。
应用程序不能直接在接口中包含的结构值上设置字段,因为接口中包含的值不可寻址。
将struct值复制到一个临时变量中,在临时变量中设置字段并将临时变量复制回接口。
var b interface{}
b = struct {
Name string
}{}
// v is the interface{}
v := reflect.ValueOf(&b).Elem()
// Allocate a temporary variable with type of the struct.
// v.Elem() is the vale contained in the interface.
tmp := reflect.New(v.Elem().Type()).Elem()
// Copy the struct value contained in interface to
// the temporary variable.
tmp.Set(v.Elem())
// Set the field.
tmp.FieldByName("Name").SetString("Hello")
// Set the interface to the modified struct value.
v.Set(tmp)
fmt.Printf("%#v\n", b)
Run Code Online (Sandbox Code Playgroud)