接口
接口
// 接口声明
type Awesomizer interface {
Awesomize() string
}
// 结构体并不需要显式实现接口
type Foo struct {}
// 而是通过实现所有接口规定的方法的方式,来实现接口
func (foo Foo) Awesomize() string {
return "Awesome!"
}
多重实现
每种类型都能实现多个接口。例如一个实现了
type Sequence []int
// Methods required by sort.Interface.
// sort.Interface 所需的方法。
func (s Sequence) Len() int {
return len(s)
}
func (s Sequence) Less(i, j int) bool {
return s[i] < s[j]
}
func (s Sequence) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Method for printing - sorts the elements before printing.
// 用于打印的方法 - 在打印前对元素进行排序。
func (s Sequence) String() string {
sort.Sort(s)
str := "["
for i, elem := range s {
if i > 0 {
str += " "
}
str += fmt.Sprint(elem)
}
return str + "]"
}
多态性
type Shape interface {
area() float64
}
func getArea(shape Shape) float64 {
return shape.area()
}
type Circle struct {
x,y,radius float64
}
type Rectangle struct {
width, height float64
}
func(circle Circle) area() float64 {
return math.Pi * circle.radius * circle.radius
}
func(rect Rectangle) area() float64 {
return rect.width * rect.height
}
func main() {
circle := Circle{x:0,y:0,radius:5}
rectangle := Rectangle {width:10, height:5}
fmt.Printf("Circle area: %f\n",getArea(circle))
fmt.Printf("Rectangle area: %f\n",getArea(rectangle))
}
//Circle area: 78.539816
//Rectangle area: 50.000000
惯用的思路是先定义接口,再定义实现,最后定义使用的方法:
package animals
type Animal interface {
Speaks() string
}
// implementation of Animal
type Dog struct{}
func (a Dog) Speaks() string { return "woof" }
/** 在需要的地方直接引用 **/
package circus
import "animals"
func Perform(a animal.Animal) { return a.Speaks() }
func funcName(a INTERFACETYPE) CONCRETETYPE
定义接口:
package animals
type Dog struct{}
func (a Dog) Speaks() string { return "woof" }
/** 在需要使用实现的地方定义接口 **/
package circus
type Speaker interface {
Speaks() string
}
func Perform(a Speaker) { return a.Speaks() }
Links
- 浅入浅出
Go 语言接口的原理: 在这一节中,我们就会介绍Go 语言中这个重要类型interface 的一些常见问题以及它底层的实现,包括接口的基本原理、类型断言和转换的过程以及动态派发机制,帮助各位Go 语言开发者更好地理解interface 类型。