首页 > 解决方案 > 通过注入类型查找切片元素的模式

问题描述

我尝试使用对象的类型在接口切片中查找对象。我目前的解决方案如下:

package main

import (
    "errors"
    "fmt"
)

type Entity struct {
    children []Childable
}

func (e *Entity) ChildByInterface(l interface{}) (Childable, error) {
    for _, c := range e.children {
        if fmt.Sprintf("%T", c) == fmt.Sprintf("%T", l) {
            return c, nil
        }
    }
    return nil, errors.New("child doesn't exist")
}

type Childable interface {
    GetName() string
}

func main() {
    ent := &Entity{
        []Childable{
            &Apple{name: "Appy"},
            &Orange{name: "Orry"},
            // more types can by introduced based on build tags
        },
    }

    appy, err := ent.ChildByInterface(&Apple{})
    if err != nil {
        fmt.Println(err)
    } else {
        appy.(*Apple).IsRed()
        fmt.Printf("%+v", appy)
    }
}

type Apple struct {
    name string
    red  bool
}

func (a *Apple) GetName() string {
    return a.name
}

func (a *Apple) IsRed() {
    a.red = true
}

type Orange struct {
    name   string
    yellow bool
}

func (o *Orange) GetName() string {
    return o.name
}

func (o *Orange) IsYellow() {
    o.yellow = true
}

https://play.golang.org/p/FmkWILBqqA-

可以使用构建标签注入更多 Childable 类型(Apple、Orange 等)。因此,为了保持查找类型的安全并避免错误,我将一个传递interface{}给查找函数。Childable 接口还确保新注入的类型实现正确的功能。

这就是事情开始变得混乱的地方。目前我正在对接口的类型和 Childable 对象的类型进行字符串比较,以查看它们是否匹配: fmt.Sprintf("%T", c) == fmt.Sprintf("%T", l)

那我还是只能返回 Childable 接口。所以我必须使用类型断言来获得正确的类型:appy.(*Apple)

锅炉电镀是为了让孩子处于正确的类型变得非常乏味,而字符串比较以找到匹配项对性能产生重大影响。我可以使用什么更好的解决方案来匹配两个接口以避免性能碰撞?

标签: gointerfacetype-assertion

解决方案


就引擎盖下的fmt.Sprintf("%T", c)使用而言reflect,没有任何优势可以暗示它 -reflect直接使用更好。您可以使用引用参数作为结果的占位符而不是返回值。

func (e *Entity) ChildByInterface(l Childable) error {
    for _, c := range e.children {
        if reflect.TypeOf(l) == reflect.TypeOf(c) {
            fmt.Println(c)
            reflect.ValueOf(l).Elem().Set(reflect.ValueOf(c).Elem())
            return nil
        }
    }
    return errors.New("child doesn't exist")
}

现在传递一个占位符

apple := &Apple{}
err := ent.ChildByInterface(apple)
//and use it
apple.IsRed()

工作代码


推荐阅读