How can two different types implement the same method in golang using interfaces?
If the printing logic depends on the interface but not on the structs themselves, then it is better to move printing to a free function that operates over an interface.
In your case, the PrintStr
method is used to print a string which is a member of each structure.
In this case, it means that each structure should implement an interface that returns the necessary string used for printing, and PrintStr
becomes a function taking a Printable
parameter.
type First struct {
str string
}
type Second struct {
str string
}
type Printable interface {
String() string
}
func (p First) String() string {
return p.str
}
func (p Second) String() string {
return p.str
}
func PrintStr(p Printable) {
fmt.Print(p.String())
}
Your use of the A
interface is non-idiomatic because an interface should not depend on the implementation of its functionality.
Instead, with this solution, you can still keep the A interface, but simplify each implementation:
func (f First) PrintStr() {
PrintStr(f)
}
func (s Second) PrintStr() {
PrintStr(s)
}
It is still redundant, but the logic lies in the function that is called from there, limiting the need to do copy-pasting in case of modification of the printing logic.
This pattern is common in the Go standard library, because many useful functions are built upon interfaces which they cannot extend, for example io.Reader.
It is a simple interface with only one method, but it is used thoroughly from many other packages.
If you look at the ioutil.ReadAll function, it could be argued that it could have been implemented as another method of the io.Reader
interface, however this keeps readers simpler, concentrating on their single method, while allowing any implementor to use ReadAll for free.
No you can't, but you could create a base type and then embed it into your 2 struct, therefore only needing an implementation for the base type:
type WithString struct {
str string
}
type First struct {
WithString
}
type Second struct {
WithString
}
type A interface {
PrintStr() //print First.str or Second.str
}
func (w WithString) PrintStr() {
fmt.Print(w.str)
}
Usage:
a := First{
WithString: WithString{
str: "foo",
},
}
Complete Example on Playground
Embed documentation