go - 通过注入类型查找切片元素的模式
问题描述
我尝试使用对象的类型在接口切片中查找对象。我目前的解决方案如下:
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)
锅炉电镀是为了让孩子处于正确的类型变得非常乏味,而字符串比较以找到匹配项对性能产生重大影响。我可以使用什么更好的解决方案来匹配两个接口以避免性能碰撞?
解决方案
就引擎盖下的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()
推荐阅读
- javascript - Vue 3:`defineProps` 正在引用本地声明的变量
- android - 应用因违反 Facebook 平台政策 6.1 而被停用(为什么它显示响应代码为 0)
- python - 如何对特定年份和月份的访问人数进行分组和统计
- php - php获取外部函数变量不起作用
- javascript - .map 是如何工作的?
- r - ggplot2 按第 80 个百分位重新排列我的箱线图
- javascript - 用于动态更新图像源的备份图像
- html - 与我的引导轮播重叠的角标题
- firebase - 如何在 Firebase 中创建计算字段?
- c - qsort() 无效的内存访问