【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>
",u.name,u.email)
}

//sendNotification 接受一个实现了notifier接口的值
//并发送通知
func sendNotification(n notifier){
	n.notify()
}


func main(){

	//user类型的值可以调用
	bill := user{"Bill","bill@email.com"}

	sendNotification(bill)


}
“年轻时,我没受过多少系统教育,但什么书都读。读得最多的是诗,包括烂诗,我坚信烂诗早晚会让我邂逅好诗。” by. 马尔克斯
原文地址:https://www.cnblogs.com/jzsg/p/10705594.html