Overriding methods in Golang
The following program:
package main
import (
"fmt"
)
type Sounder interface {
Sound()
}
// base
type Animal struct {
Sounder
}
func (a Animal) Sound() {
fmt.Println("Pruttel")
}
// 'inherit'
type Duck struct {
Animal
}
// override
func (d Duck) Sound() {
fmt.Println("Quaeck!")
}
func main() {
// speak to interfaces not implementations
var animal Sounder = Animal{}
animal.Sound()
var duck Sounder = Duck{}
duck.Sound()
}
Yields this output:
Pruttel
Quaeck!
Try it out on the Golang playground.