golang.GoInAction.5-62

//《Go 语言实战》 p110_listing60.go
//这个示例程序展示当内部类型和外部类型要
//实现同一个接口的方法
package main
import (
    "fmt"
)
//notifier 是一个定义了
//通知类行为的接口
type notifier interface {
    notify()
}
//user 在程序里定义了一个用户类型
type user struct {
    name string
    email string
}
//通过user类型的指针
//调用的方法
func (u *user) notify() {
    fmt.Printf("Sending user email to %s<%s> ",
        u.name,
        u.email)
}
//admin 代表一个拥有权限的管理员用户
type admin struct {
    user
    level string
}
//通过admin类型的指针
//调用的方法
func (s *admin) notify() {
    fmt.Printf("Sending admin email to %s<%s> ",
        s.name,
        s.email)
}
//main是应用程序的入口
func main() {
    //创建一个admin用户
    ad := admin{
        user: user{
            name: "john smith",
            email: "john@yahoo.com",
        },
        level: "super",
    }
    //给admin用户发送一个通知
    //接口的嵌入的内部类型实现并没有提升到
    //外部类型
    sendNotification(&ad)
    //我们可以直接访问内部类型的方法
    ad.user.notify()
    //内部类型的方法没有被提升
    ad.notify()
}
//sendNotification接受一个实现了notifier接口的值
//并发送通知
func sendNotification(n notifier) {
    n.notify()
}
 
//outprint
//Sending admin email to john smith<john@yahoo.com>
//Sending user email to john smith<john@yahoo.com>
//Sending admin email to john smith<john@yahoo.com>
原文地址:https://www.cnblogs.com/ycliu912/p/10074529.html