一起来学 Go (6)Interface

1. 面向接口编程

1.1 特征

  • 模块解耦
1
2
3
4
5
6
7
type Dog struct {
    times int
}

func (d Dog) Call() {
    d.times = d.times + 1 // not work
}
  • 指针类型的 Receiver
1
2
3
4
5
6
7
type Dog struct {
    times int
}

func (d *Dog) Call() {
    d.times = d.times + 1 // work
}