dongqiang2358 2019-09-07 12: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 12: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部