The FAQ is mentioned
Experience with other languages ββtells us that using different methods with the same name but with different signatures was sometimes useful, but could also be confusing and fragile in practice. Only name matching and type consistency was the main simplifying solution in a Go type system .
In your case, you will satisfy both interfaces.
You can check whether an object (type of interface) corresponds to another type of A.Doer interface by doing:
if _, ok := obj.(A.Doer); ok { }
OP adds:
But the logic implemented in the Do method to satisfy A is completely different from the one in B
Then you need to implement a wrapper around the object:
- a
DoerA that has its C object as a field and implements A.Do() in a way that satisfies how A.Do() should work - a
DoerB that has the same C object as a field and implements B.Do() in a way that satisfies how B.Do() should work
This way you will know which Doer will go to a function that expects A.Doer or B.Doer .
You will not need to implement the Do() method on your original C object, which will not be able to handle the different logic of A.Do() and B.Do() .
source share