gpt4 book ai didi

go - 当值是 reflect.Ptr 时如何使用 reflect.FieldByName

转载 作者:数据小太阳 更新时间:2023-10-29 03:22:25 35 4
gpt4 key购买 nike

我有一个 project 函数,它返回一个 slice ,其中包含输入 slice 中每个结构或映射的名称的字段值。我遇到输入 slice 包含指向结构的指针的情况。我已经设置了一个递归函数来对值进行操作,但需要知道如何将类型 reflect.Ptr 转换为底层 reflect.Struct。这是怎么做到的?任何其他设计建议表示赞赏。我对 Go 还是有点陌生​​。

代码如下:

func project(in []interface{}, property string) []interface{} {

var result []interface{}

var appendValue func(list []interface{}, el interface{})

appendValue = func(list []interface{}, el interface{}) {
v := reflect.ValueOf(el)
kind := v.Kind()
if kind == reflect.Ptr {

// How do I get the struct behind this ptr?
// appendValue(list, el)

} else if kind == reflect.Struct {
result = append(result, v.FieldByName(property).Interface())
} else if kind == reflect.Map {
result = append(result, el.(map[string]interface{})[property])
} else {
panic("Value must be a struct or map")
}
}

for _, el := range in {
appendValue(result, el)
}

return result

}

...和测试用例:

func Test_project(t *testing.T) {

cases := map[string]struct {
input []interface{}
property string
expected []interface{}
}{
"simple-map": {
[]interface{}{
map[string]interface{}{
"a": "a1",
},
},
"a",
[]interface{}{"a1"},
},
"simple-struct": {
[]interface{}{
simpleStruct{
A: "a1",
},
},
"A",
[]interface{}{"a1"},
},
// THIS ONE FAILS
"simple-struct-ptr": {
[]interface{}{
&simpleStruct{
A: "a1",
},
},
"A",
[]interface{}{"a1"},
},
}

for k, v := range cases {

t.Run(k, func(t *testing.T) {
got := project(v.input, v.property)
if !reflect.DeepEqual(got, v.expected) {
t.Fatalf("Expected %+v, got %+v", v.expected, got)
}
})
}
}

最佳答案

使用Elem()reflect.Ptr 到它指向的值。

关于go - 当值是 reflect.Ptr 时如何使用 reflect.FieldByName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51470090/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com