3 ms·
Animal 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
by rakoo 3y ago
Animal 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!
}