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\"
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()
}