我已经阅读了几个关于 Go 中反射的示例/问题,但我仍然无法理解我应该如何处理我的接口(interface)列表。
以下是真实用例的精简版。
我有几种符合给定接口(interface)的类型:
type Foo interface {
Value() int
}
type bar struct {
value int
}
func (b bar) Value() int {
return b.value
}
type baz struct{}
func (b baz) Value() int {
return 42
}
我有一个这样的人的名单
type Foos []Foo
var foos = Foos{
bar{},
baz{},
}
我想通过更改具有 value
字段的成员的值来遍历此列表。
for k := range foos {
change(&foos[k])
}
但是我找不到正确的路径
func change(g *Foo) {
t := reflect.TypeOf(g).Elem()
fmt.Printf("t: Kind %v, %#v\n", t.Kind(), t)
v := reflect.ValueOf(g).Elem()
fmt.Printf("v: Kind %v, %#v, %v\n", v.Kind(), v, v.CanAddr())
if f, ok := t.FieldByName("value"); ok {
fmt.Printf("f: %#v, OK: %v\n", f, ok)
vf := v.FieldByName("value")
fmt.Printf("vf: %#v: %v\n", vf, vf.CanAddr())
vf.SetInt(51)
}
}
如您所见,我不确定如何将 TypeOf 和 ValueOf 位粘合在一起...
最佳答案
这里有几个问题。首先,不可能设置unexported。 field 。这是使字段导出的更改:
type Foo interface {
// Rename Value to GetValue to avoid clash with Value field in bar
GetValue() int
}
type bar struct {
// export the field by capitalizing the name
Value int
}
func (b bar) GetValue() int {
return b.Value
}
type baz struct{}
func (b baz) GetValue() int {
return 42
}
下一个问题是 bar
接口(interface)值不可寻址。要解决此问题,请在 slice 中使用 *bar
而不是 bar
:
func (b *bar) GetValue() int {
return b.Value
}
...
var foos = Foos{
&bar{},
baz{},
}
有了这些变化,我们可以编写函数来设置值:
func change(g Foo) {
v := reflect.ValueOf(g)
// Return if not a pointer to a struct.
if v.Kind() != reflect.Ptr {
return
}
v = v.Elem()
if v.Kind() != reflect.Struct {
return
}
// Set the field Value if found.
v = v.FieldByName("Value")
if !v.IsValid() {
return
}
v.SetInt(31)
}
以上回答了问题,但未必是解决实际问题的最佳方案。更好的解决方案可能是定义一个 setter 接口(interface):
type ValueSetter interface {
SetValue(i int)
}
func (b *bar) Value() int {
return b.value
}
func (b *bar) SetValue(i int) {
b.value = i
}
func change(g Foo) {
if vs, ok := g.(ValueSetter); ok {
vs.SetValue(31)
}
}
关于go - 反射(reflect)接口(interface)列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50217748/