3

我有一个类型的变量,interface{}我想使用反射更改字段的值。我该怎么做?interface{}由于其他要求,变量必须是类型。如果变量的类型不是interface{}all works,否则代码抛出

reflect: call of reflect.Value.FieldByName on interface Value

我的代码

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)
}

4

2 回答 2

8

应用程序必须调用Elem()两次才能获取结构值:

reflect.ValueOf(&b).Elem().Elem().FieldByName("Name").SetString("Hello")

第一次调用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)

在 Go 操场上运行它

于 2020-08-15T02:33:57.137 回答
1

该接口b使用匿名结构的值进行初始化,因此b包含该结构的副本,并且这些值是不可寻址的。b使用指针初始化:

var b interface{}
    b = &struct {
        Name string
    }{}
    reflect.ValueOf(b).Elem().FieldByName("Name").SetString("Hello")
于 2020-08-15T02:35:42.317 回答