I'm new to go and I wish to make two packages with (very) loosely coupled API between a uses and a provider. For this, I wish to use go's ability to implicitly implement interfaces and implicit conversion.
Both the provider and the user have their own defined interfaces (for exemple provider returns a provider.A and user accepts a user.A). With this pattern, I can convert from one type to the other instead of importing the interfaces from another package.
This works fine with simple interfaces, but as soon as a method takes an interfaces as an input, the conversion from one type to the other become impossible.
Why doesn't go allow this kind of conversion? Is there any workaround?
working example:
package main
// Provider
type A interface{
AddString(string)
}
type a struct{
b string
}
func (a *a) AddString(b string) {
a.b = b
}
func NewA() A {
return &a{nil}
}
// User
type A2 interface{
AddString(string)
}
func Main() {
var _ A2 = NewA()
}
example that cause problems:
package main
// Provider
type A interface{
AddB(B)
}
type B interface{}
type a struct{
b B
}
func (a *a) AddB(b B) {
a.b = b
}
func NewA() A {
return &a{nil}
}
// User
type A2 interface{
AddB(B2)
}
type B2 interface{}
func Main() {
var _ A2 = NewA() // error..
}