英文:
Creating a map of constructor functions
问题
我想要一个函数名称的映射表,根据环境变量选择接口的实现方式。我在下面的代码中重现了这个问题:
package test
type Fooer interface {
Foo() error
}
type Bar struct{}
func NewBar() (*Bar, error) { return &Bar{}, nil }
func (b *Bar) Foo() error { return nil }
type Baz struct{}
func NewBaz() (*Baz, error) { return &Baz{}, nil }
func (b *Baz) Foo() error { return nil }
var constructors map[string]func() (*Fooer, error)
func init() {
constructors = map[string]func() (*Fooer, error){
"bar": NewBar,
"baz": NewBaz,
}
}
当我运行go build test.go
时,会出现以下错误:
./test.go:21: cannot use NewBar (type func() (*Bar, error)) as type func() (*Fooer, error) in map value
./test.go:22: cannot use NewBaz (type func() (*Baz, error)) as type func() (*Fooer, error) in map value
那么我做错了什么?我能够将*Fooer
作为构造函数的返回类型吗?实际上,最好的解决方法是什么?(我是Go的新手)
英文:
So I'd like to have a map of names of functions, to choose an implementation of an interface based on an environment variable. I've reproduced it in the following code:
package test
type Fooer interface {
Foo() error
}
type Bar struct{}
func NewBar() (*Bar, error) { return &Bar{}, nil }
func (b *Bar) Foo() error { return nil }
type Baz struct{}
func NewBaz() (*Baz, error) { return &Baz{}, nil }
func (b *Baz) Foo() error { return nil }
var constructors map[string]func() (*Fooer, error)
func init() {
constructors = map[string]func() (*Fooer, error){
"bar": NewBar,
"baz": NewBaz,
}
}
This throws the following errors when I go build test.go
:
./test.go:21: cannot use NewBar (type func() (*Bar, error)) as type func() (*Fooer, error) in map value
./test.go:22: cannot use NewBaz (type func() (*Baz, error)) as type func() (*Fooer, error) in map value
So what am I doing wrong? Can I use a *Fooer
as the return type of a constructor function? What would be the actual best way to approach this? (I'm new to Go)
答案1
得分: 2
- 不要(几乎不要)传递指向接口的指针。你(几乎)不需要一个
*Fooer
,一个Fooer
就足够了。 - 是的,你的 Bar 和 Baz 构造函数可以返回一个
Fooer
(而不是*Fooer
!),但这似乎有些尴尬。 - 将构造函数保存在一个映射中,并查询映射以获取构造函数,似乎有一层过多的间接性。你是否经常创建新的 Bar 和 Baz?
英文:
- Do not (almost never) pass around pointers to interface. You will (almost) never need a
*Fooer
, aFooer
is enough. - Yes, your Bar and Baz constructors could return a
Fooer
(not a*Fooer
!) but that seems awkward. - Keeping the constructors in a map and querying the map for a constructor seem like one level of indirection too much. Are you constantly creating new Bar and Baz?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论