2 ms·
But then Dog and Cat need to both contain [edit] explicitly reproduce, verbatim [/edit] all the common functions of "Animal" - even if they're exactly the same?
by noduerme 4y ago
But then Dog and Cat need to both contain [edit] explicitly reproduce, verbatim [/edit] all the common functions of "Animal" - even if they're exactly the same? Just so someone later can take Dog and reuse it without implementing Animal?
- cube2222 4y agoYou can use struct embedding to accomplish that if you need it (Embed Dog / Animal in a new struct which adds/modifies functionality). Though this is used fairly rarely in Go.
- rakoo 4y agoAnimal is an interface: it has no "common functions", they're just definitions. If you need a common function, what you want is another structure, say Pet, and use structure embedding type Pet struct { func IsCute() bool {return yes} } type Dog struct { Pet # note how it is not named } func (Dog) MakeNoise() { fmt.Println("Woof!") } func main() { d := Dog{} d.IsCute() # yes # A third party lib type Chihuaha struct { Dog } c := Chihuaha {} c.IsCute() # yes c.MakeNoise() # Woof! }