创建一个构造函数的映射表

huangapple go评论153阅读模式
英文:

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

  1. 不要(几乎不要)传递指向接口的指针。你(几乎)不需要一个 *Fooer,一个 Fooer 就足够了。
  2. 是的,你的 Bar 和 Baz 构造函数可以返回一个 Fooer(而不是 *Fooer!),但这似乎有些尴尬。
  3. 将构造函数保存在一个映射中,并查询映射以获取构造函数,似乎有一层过多的间接性。你是否经常创建新的 Bar 和 Baz?
英文:
  1. Do not (almost never) pass around pointers to interface. You will (almost) never need a *Fooer, a Fooer is enough.
  2. Yes, your Bar and Baz constructors could return a Fooer (not a *Fooer!) but that seems awkward.
  3. 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?

huangapple
  • 本文由 发表于 2013年12月10日 20:22:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/20494445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定