扩展与复用
package extension
import (
"fmt"
"testing"
)
type Pet struct {
}
func (p *Pet) Speak() {
fmt.Print("...")
}
func (p *Pet) SpeakTo(host string) {
p.Speak()
fmt.Println(" ", host)
}
type Dog struct {
p *Pet
}
func (d *Dog) Speak() {
fmt.Print("Wang!")
}
func (d *Dog) SpeakTo(host string) {
d.Speak()
fmt.Println(" ", host)
}
func TestDog(t *testing.T) {
dog:=new(Dog)
dog.SpeakTo("nikko")
}
输出
=== RUN TestDog
Wang! nikko
--- PASS: TestDog (0.00s)
PASS
Process finished with exit code 0
示例代码请访问: github.com/wenjianzhan…