是否有可能在另一个包中实现一个带有未导出方法的接口?

编程入门 行业动态 更新时间:2024-10-26 05:31:23
本文介绍了是否有可能在另一个包中实现一个带有未导出方法的接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我写了一个会计系统访问接口。我想从我的程序中隐藏接口的特定实现,因为我将只有一个活动会计系统。所以我计划让接口的方法不导出(隐藏),然后导出基本包的本地函数,从本地适配器调用相同的函数。

<$ $ b var adapter IAdapter func SetAdapter(一个IAdapter){ adapter = a } func GetInvoice()错误{ if(adapter == nil){ return errors.New(No adapter set!)} return adapter.getInvoice()} __________________________________________________ 包裹帐户系统 类型适配器struct {} func(一个适配器)getInvoice( )错误{return nil} __________________________________________________ 包裹主要 进口(会计 accountingsystem) 函数m ain(){ adapter:= accountingsystem.Adapter {} accounting.SetAdapter(adapter)}

问题在于编译器抱怨,由于无法通过<$ 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() error

Is 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() }

更多推荐

是否有可能在另一个包中实现一个带有未导出方法的接口?

本文发布于:2023-06-08 03:51:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/576138.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有可能   包中   接口   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!