Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 反思接口列表_Go_Reflection_Interface - Fatal编程技术网

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

我已经阅读了几个关于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 {
    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)
    }
}

请不要使用当前的方法。它有几个缺点

  • 这让人困惑(正如你所发现的)
  • 很慢。反思总是缓慢的
  • 这是不灵活的。您的循环,而不是类型本身,必须了解每个类型实现的功能
  • 相反,只需扩展接口以包含setter方法——可能是可选的setter方法。然后,您可以循环遍历这些项,并为支持它的项设置值。例如:

    // 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
    时,您根本不需要修改循环。

    请不要使用当前方法。它有几个缺点

  • 这让人困惑(正如你所发现的)
  • 很慢。反思总是缓慢的
  • 这是不灵活的。您的循环,而不是类型本身,必须了解每个类型实现的功能
  • 相反,只需扩展接口以包含setter方法——可能是可选的setter方法。然后,您可以循环遍历这些项,并为支持它的项设置值。例如:

    // 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
    时,您根本不需要修改循环。

    我不是必须处理的类型和接口的作者,并且无法更改它们。否则,向上游做这件事,就像你做的那样,当然是一种方法,毫无疑问。谢谢我不是必须处理的类型和接口的作者,也不能更改它们。否则,向上游做这件事,就像你做的那样,当然是一种方法,毫无疑问。谢谢