例子
package main
import "fmt"
//定義一個 anial 接口類型
type anial interface {
move()
}
type cat struct {
name string
feet int
}
type dog struct {
name string
feet int
}
// 采用值接收者遗嗽,參數(shù)可以傳遞結構體類型和結構體指針類型
//func (c cat)move() {
// fmt.Printf("%s,走貓步\n", c.name)
//}
//
//func (d dog)move() {
// fmt.Printf("%s,走狗步\n", d.name)
//}
// 采用指針接收者肺然,參數(shù)僅可以傳遞結構體指針類型
func (c cat)move() {
fmt.Printf("%s,走貓步\n", c.name)
}
func (d dog)move() {
fmt.Printf("%s,走狗步\n", d.name)
}
func main() {
var a anial
c1 := cat{
name:"Tom",
feet:4,
}
a = &c1
a.move()
a = &dog{
name:"wawa",
feet:4,
}
a.move()
}