【Duke】扩展与复用 015 - PingPongGooo/GoFoundation GitHub Wiki

面向对象的扩展,可以用 继承 或者 组合来实现 Go语言不支持继承

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 {
    Pet   
}

func (d *dog)Speak(){
   fmt.Print("wang!")
}

func TestDog(t *testing.T){
	dog := new(Dog)
	dog.SpeakTo("Chao") 
}

打印: ... Chao