Go code
type Speaker interface { Speak() string }
type Dog struct{}
func (Dog) Speak() string { return "Woof" }
func announce(s Speaker) { fmt.Println(s.Speak()) }
announce(Dog{})Line-by-line explanation
- Speaker requires Speak.
- Dog is a concrete type.
- Dog has the required method automatically.
- Announce accepts any Speaker.
- A Dog value is passed.
Expected output
WoofThis is the expected result.