If I am understanding you correctly, you're saying your implementation is coupled to the config
struct instead of the interface. If that is the case, simply replace the argument that uses the config
to the interface, in this case PAAPI
.
In the below code, instead of the DoSomethingWithStruct
function definition, what you want is probably the DoSomethingWithInterface
function definition.
type PAAPI interface {
Foo() // just a stub method
}
type Config struct {}
func (config *Config) Foo() {
// do something
}
func DoSomethingWithStruct(config Config) {
// do something
}
// you probably want a method that uses your config through the interface
func DoSomethingWithInterface(config PAAPI) {
// do something
}
To take it another step further, if in an external package, you want to implement the interface simply define another struct that adheres to the interface. Like the below:
type ExternalConfig struct{}
func (config *ExternalConfig) Foo() {
// do something
}
A couple notes about this that differs from Java is that there is no explicit implements
keyword in Go. It is based off a familiar principle in dynamically typed programming languages instead, which is basically duck-typing. It implicitly knows that the interface is implemented by the struct at compile time.
Based on the two previous snippets of code, now you can call DoSomethingWithInterface(ExternalConfig{})
. Note that I inlined the instance of ExternalConfig
here but you can create it with actual credentials and pass it into DoSomethingWithInterface
in the same way. Hope this helps. Also, feel free to comment to ask for clarity if I'm not quite hitting the target.