我从简单的界面开始:
type Module interface {
Init(deps ...interface{}) error
}
我想,实现会非常简单,因为这个方法应该匹配任意数量的提供参数。这就是我最终用这段代码思考的内容,TestModule实现了Module接口。
type TestModule struct {
}
func (m *TestModule) Init(str string) error {
return nil
}
但是当我想将 TestModule 传递给任何需要 Module 的函数时,我收到此错误:
不能在 testFunc 的参数中使用 module(type *TestModule) 作为类型 Module:
func testFunc(module Module) {
}
编辑:是否有任何最佳实践来实现这种行为?
慕的地6264312
相关分类