鸭子输入:如何在Go中从一个接口隐式转换为另一个接口

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..
}

Go provides type checking as a feature, not a bug. When you declare B and B2 to be separate types, the compiler respects this distinction and treats them differently.

A familiar example is time.Duration, which is just an int64 to count nanoseconds, but you cannot mix and match variables that are int64 and time.Duration without an explicit type conversion. see time.Duration docs

Rather than trying to work around Go's type checking feature, you should try to learn how to express your algorithm using idiomatic Go.