mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 02:16:48 +08:00
update book code
This commit is contained in:
32
eBook/examples/chapter_11/duck_dance.go
Normal file
32
eBook/examples/chapter_11/duck_dance.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type IDuck interface {
|
||||
Quack()
|
||||
Walk()
|
||||
}
|
||||
|
||||
func DuckDance(duck IDuck) {
|
||||
for i := 1; i <= 3; i++ {
|
||||
duck.Quack()
|
||||
duck.Walk()
|
||||
}
|
||||
}
|
||||
|
||||
type Bird struct {
|
||||
// ...
|
||||
}
|
||||
|
||||
func (b *Bird) Quack() {
|
||||
fmt.Println("I am quacking!")
|
||||
}
|
||||
|
||||
func (b *Bird) Walk() {
|
||||
fmt.Println("I am walking!")
|
||||
}
|
||||
|
||||
func main() {
|
||||
b := new(Bird)
|
||||
DuckDance(b)
|
||||
}
|
Reference in New Issue
Block a user