【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","[email protected]"}

    sendNotification(bill)


}

猜你喜欢

转载自www.cnblogs.com/jzsg/p/10705594.html