【GO】golang 中的 interface

摘录自《Go语言实战》

package main

import "fmt"


type notifier interface {
	notify()
}

//go 的struct是值类型
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类型的值可以调用
	bill := user{"Bill","bill@email.com"}

	sendNotification(bill)


}
posted @ 2019-04-14 16:12  加州水果  阅读(391)  评论(0编辑  收藏  举报