Go 反思接口列表
我已经阅读了几个关于Go中反射的示例/问题,但我仍然无法理解我应该如何处理我的接口列表 下面是真实用例的精简版本 我有几种符合给定接口的类型:Go 反思接口列表,go,reflection,interface,Go,Reflection,Interface,我已经阅读了几个关于Go中反射的示例/问题,但我仍然无法理解我应该如何处理我的接口列表 下面是真实用例的精简版本 我有几种符合给定接口的类型: 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 { retu
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 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)
}
}
我有一份这样的人的名单
type Foos []Foo
var foos = Foos{
bar{},
baz{},
}
我想通过更改具有值
字段的成员的值来遍历此列表
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)
}
}
正如你所看到的,我不知道如何将位的类型和值粘合在一起
这里有几个问题。首先,无法设置字段。以下是将字段导出的更改:
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
}
下一个问题是条
接口值不可寻址。要解决此问题,请在切片中使用*条
,而不是条
:
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接口:
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 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)
}
}
这里有几个问题。首先,无法设置字段。以下是将字段导出的更改:
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
}
下一个问题是条
接口值不可寻址。要解决此问题,请在切片中使用*条
,而不是条
:
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接口:
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 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)
}
}
请不要使用当前的方法。它有几个缺点
// FooSetter is an optional interface which allows a Foo to set its value.
type FooSetter interface {
SetValue(int)
}
// SetValue implements the optional FooSetter interface for type bar.
func (b *bar) SetValue(v int) {
b.value = v
}
然后使您的循环如下所示:
for _, v := range foos {
if setter, ok := v.(FooSetter); ok {
setter.SetValue(51)
}
}
现在,当您(或库的第三方用户)添加满足
FooSetter
界面的类型Baz
时,您根本不需要修改循环。请不要使用当前方法。它有几个缺点
// FooSetter is an optional interface which allows a Foo to set its value.
type FooSetter interface {
SetValue(int)
}
// SetValue implements the optional FooSetter interface for type bar.
func (b *bar) SetValue(v int) {
b.value = v
}
然后使您的循环如下所示:
for _, v := range foos {
if setter, ok := v.(FooSetter); ok {
setter.SetValue(51)
}
}
现在,当您(或库的第三方用户)添加满足
FooSetter
接口的类型Baz
时,您根本不需要修改循环。我不是必须处理的类型和接口的作者,并且无法更改它们。否则,向上游做这件事,就像你做的那样,当然是一种方法,毫无疑问。谢谢我不是必须处理的类型和接口的作者,也不能更改它们。否则,向上游做这件事,就像你做的那样,当然是一种方法,毫无疑问。谢谢