点击运行
package main import "fmt" type Describer interface { Describe() } type Person struct { name string age int } func (p Person) Describe() { // 使用值接收器实现接口 fmt.Printf("%s is %d years old\n", p.name, p.age) } type Address struct { state string country string } func (a *Address) Describe() { // 使用指针接收器实现接口 fmt.Printf("State %s Country %s", a.state, a.country) } func main() { var d1 Describer p1 := Person{"Sam", 25} d1 = p1 d1.Describe() p2 := Person{"James", 32} d1 = &p2 d1.Describe() var d2 Describer a := Address{"Tom", "USA"} d2 = a // d2 = &a // 此行代码可以正常执行,因为Describer是由Address 指针实现的 d2.Describe() }
运行结果 :
正在执行...