// Chapter 29 example: an interface lets one slice hold different types. // Run with: go run exercises/29/03-interface.go package main import "fmt" type describer interface { describe() } type animal struct { name string } func (a animal) describe() { fmt.Printf("%s is an animal.\n", a.name) } type dog struct { animal } func (d dog) describe() { fmt.Printf("%s is a dog.\n", d.name) } type cat struct { animal } func (c cat) describe() { fmt.Printf("%s is a cat.\n", c.name) } type parrot struct { animal } func introduce(d describer) { fmt.Print("Meet this one: ") d.describe() } func main() { rex := dog{animal: animal{name: "Rex"}} tom := cat{animal: animal{name: "Tom"}} polly := parrot{animal: animal{name: "Polly"}} introduce(rex) creatures := []describer{rex, tom, polly, animal{name: "Bug"}} for _, c := range creatures { c.describe() } }