github.com/nibnait/go-learn@v0.0.0-20220227013611-dfa47ea6d2da/src/test/chapter/ch2/17_extension_test.go (about) 1 package ch2 2 3 import ( 4 "fmt" 5 "testing" 6 ) 7 8 type Pet struct { 9 } 10 11 func (p *Pet) speak() { 12 fmt.Print("...") 13 } 14 15 func (p *Pet) speakTo(host string) { 16 p.speak() 17 fmt.Println(" ", host) 18 } 19 20 type Dog struct { 21 Pet 22 } 23 24 func (d *Dog) speak() { 25 fmt.Println("汪!汪!") 26 } 27 28 func TestDog(t *testing.T) { 29 dog := new(Dog) 30 dog.speakTo("tian") 31 }