Overriding methods in Golang
Given the following program:
package mainimport ("fmt")type Sounder interface {Sound()}// basetype Animal struct {Sounder}func (a Animal) Sound() {fmt.Println("Pruttel")}// 'inherit'type Duck struct {Animal}// overridefunc (d Duck) Sound() {fmt.Println("Quaeck!")}func main() {// speak to interfaces not implementationsvar animal Sounder = Animal{}animal.Sound()var duck Sounder = Duck{}duck.Sound()}
yields this output:
PruttelQuaeck!
Try it out on the Golang playground.