dongqiang2358 2019-09-07 20:27
浏览 24
已采纳

如何使用反射将值分配给嵌套结构

***see updated field at the bottom,please****

I want to assign a value to a nested struct but it does not work:

try code here

 type MyStruct2 struct {
    Value3 string
}
type myStruct1 struct {
    Value1    string
    Value2    int
    MyStruct2 MyStruct2
}

func main() {
    var ah myStruct1 
    t := reflect.TypeOf(ah)
    where := reflect.ValueOf(&ah).Elem()

    rt0 := t.Field(0)
    field0 := where.FieldByIndex(rt0.Index)
    field0.SetString("hello") 

    rt1 := t.Field(1)
    field1 := where.FieldByIndex(rt1.Index)
    field1.SetInt(4)

    rt2 := t.Field(2)
    rt2_1:=rt2.Type.Field(0)
    field2 := where.FieldByIndex(rt2_1.Index)
    field2.SetString("hello2")//not assigning to struct

    fmt.Printf("%+v
",ah)
}

output: {Value1:hello2 Value2:4 MyStruct2:{Value3:}}

As you can see, it's not assigning a value to the nested struct

// update:

as @Austin said this is solved by using:

field2 :=where.FieldByIndex(rt2.Index).FieldByIndex(rt2_1.Index)

instead, but it's not working inside function:

try code here

type MyStruct2 struct {
    Value3 string
}
type myStruct1 struct {
    Value1    string
    Value2    string
    MyStruct2 MyStruct2
}

func main() {
    var ah myStruct1
    t := reflect.TypeOf(ah)
    where := reflect.ValueOf(&ah).Elem()
    max := t.NumField()
    for i := 0; i < max; i++ {
        f := t.Field(i)
        findAssing("hello", f, where)
    }
    fmt.Printf("%+v
", ah)
}

func findAssing(me string, rt reflect.StructField, field reflect.Value) {
    if rt.Type.Kind() == reflect.Struct {
        max := rt.Type.NumField()
        for i := 0; i < max; i++ {
            if rt.Type.Field(i).Type.Kind() == reflect.Struct {
                field = field.FieldByIndex(rt.Type.Field(i).Index)
            }
            findAssing(me, rt.Type.Field(i), field)
        }
    } else {
        field = field.FieldByIndex(rt.Index)
        field.SetString("hello")
    }
}
  • 写回答

2条回答 默认 最新

  • drvfqr5609 2019-09-07 20:44
    关注

    You need to select the third field of the outer struct before you select the first field of the inner struct. I.e.,

    rt2 := t.Field(2)
    rt2_1 := rt2.Type.Field(0)
    field2 := where.FieldByIndex(rt2.Index).FieldByIndex(rt2_1.Index)
    field2.SetString("hello2") // Will assign to the inner struct now.
    

    Edit: The code in the edited question makes a similar mistake in that it does not fetch the field of the outer struct first. Additionally, it overrides the value of field repeatedly in a way that probably doesn't do what is intended. Something like this should work:

    func findAssing(me string, rt reflect.StructField, field reflect.Value) {
        subField := field.FieldByIndex(rt.Index)
        if rt.Type.Kind() == reflect.Struct {
            max := rt.Type.NumField()
            for i := 0; i < max; i++ {
                findAssing(me, rt.Type.Field(i), subField)
            }
        } else {
            subField.SetString("hello")
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用