2
所以我想要有一個函數名稱的映射來選擇基於環境變量的接口的實現。我在下面的代碼複製它:創建構造函數的地圖
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)
重新3:只是一次,而我設置服務器的配置。需要選擇使用哪個存儲實施。 –
另外,我修改它不返回一個指針,我得到相同的錯誤(模塊指針,顯然。)我錯過了什麼? –
比一個簡單的'var storage Fooer;如果init中的environmentWantsBaz {storage,_ = Baz {}} else {storage,_ = Bar {}}'應該這樣做。 – Volker