从接收者类型的角度来看方法集


//从接收者类型的角度来看方法集
Methods Receivers   Values
(t T)           	T and *T
(t *T)				*T
如果使用指针接收者来实现一个接口,那么只有指向那个类型的指针才能够实现对应的接口。
如果使用值接收者来实现一个接口,那么那个类型的值和指针都能够实现对应的接口
// 示例
package main
import (
	"fmt"
)
// 定义接口
type notifier interface{
    notify()
}
// 定义user结构体
type user struct{
    name string
    email string
}
// 使用指针接收者实现的方法
func (u *user) notify(){
    fmt.Printf("Sending user email to %s<%s>\n", u.name, u.email)
}
//sendNotification接收一个实现了notifier接口的值
func sendNotification(n notifier){
    n.notify()
}

// 应用程序入口
func main(){
    // 创建一个user类型的值
    u := user{"Bill", "bill@email.com"}
    //sendNotification(u)  // 这边会报错
    sendNotification(&u) // 正确写法传指针
}