问题在于编译器抱怨,由于无法通过<$ c查看 getInvoice()的实现$ c> accountingsystem.Adapter :
./ main.go:2:can not use adapter类型accountingsystem.Adapter)作为类型accounting.IAdapter在accounting.SetAdapter的参数: accountingsystem.Adapter没有实现accounting.IAdapter(缺少accounting.getInvoice方法)有accountingsystem.getInvoice()错误想accounting.getInvoice()错误是否有任何方法能够实现一个接口在另一个软件包中未导出的方法?或者我是用非惯用的方式来思考这个问题?
解决方案字段,但不能提供您自己的未导出方法的实现。例如,此版本的Adapter满足accounting.IAdapter接口。
类型适配器结构{ accounting.IAdapter $ b我无法使用适配器来提供我自己的IAdapter实现。 getInvoice()方法。
这个技巧不会帮助你。
如果你不想要其他软件包直接使用accountingsystem.Adapter,然后使类型不导出,并添加一个函数用于向适配器注册会计软件包。
包计费 类型IAdapter接口{ GetInvoice()错误} --- 包会计系统 类型适配器struct {} func(一个适配器)GetInvoice()错误{return nil} func SetupAdapter(){ accounting.SetAdapter(adapter {})} --- 包主 func main(){ accounti ngsystem.SetupAdapter()}
I have written an interface for accounting system access. I would like to hide specific implementations of the interface from my program as I will only ever have one "active" accounting system. So I planned on making the methods of the interface unexported (hidden), and then having exported functions native to the base package that call a the same function from a local adapter.
package accounting import "errors" type IAdapter interface { getInvoice() error } var adapter IAdapter func SetAdapter(a IAdapter) { adapter = a } func GetInvoice() error { if (adapter == nil) { return errors.New("No adapter set!") } return adapter.getInvoice() } __________________________________________________ package accountingsystem type Adapter struct {} func (a Adapter) getInvoice() error {return nil} __________________________________________________ package main import ( "accounting" "accountingsystem" ) function main() { adapter := accountingsystem.Adapter{} accounting.SetAdapter(adapter) }The problem with this is that the compiler complains, due to not being able to see the implementation of getInvoice() by accountingsystem.Adapter:
./main.go:2: cannot use adapter (type accountingsystem.Adapter) as type accounting.IAdapter in argument to accounting.SetAdapter: accountingsystem.Adapter does not implement accounting.IAdapter (missing accounting.getInvoice method) have accountingsystem.getInvoice() error want accounting.getInvoice() errorIs there any way of being able to implement an interface with unexported methods in another package? Or am I thinking about this problem in a non-idiomatic way?
解决方案You can implement an interface with unexported methods using anonymous struct fields, but you cannot provide your own implementation of the unexported methods. For example, this version of Adapter satisfies the accounting.IAdapter interface.
type Adapter struct { accounting.IAdapter }There's nothing that I can do with Adapter to provide my own implementation of the IAdapter.getInvoice() method.
This trick is not going to help you.
If you don't want other packages to use accountingsystem.Adapter directly, then make the type unexported and add a function for registering the adapter with the accounting package.
package accounting type IAdapter interface { GetInvoice() error } --- package accountingsystem type adapter struct {} func (a adapter) GetInvoice() error {return nil} func SetupAdapter() { accounting.SetAdapter(adapter{}) } --- package main func main() { accountingsystem.SetupAdapter() }
更多推荐
是否有可能在另一个包中实现一个带有未导出方法的接口?
发布评论