书上看的。慢慢领会。。

package main

import (
	"fmt"
)

type notifier interface {
	notify()
}

type user struct {
	name  string
	email string
}

func (u *user) notify() {
	fmt.Printf("Sending user email to %s<%s>\n",
		u.name,
		u.email)
}

type admin struct {
	user
	level string
}

func (a *admin) nofity() {
	fmt.Printf("Sending admin email to %s<%s>\n",
		a.name,
		a.email,
		a.level)
}

func main() {
	ur := user{
		name:  "Bill smith",
		email: "[email protected]",
	}

	ad := admin{
		user: user{
			name:  "John smith",
			email: "[email protected]",
		},
		level: "super",
	}

	sendNotification(&ur)
	sendNotification(&ad)

	ad.user.notify()
	ad.nofity()

}

func sendNotification(n notifier) {
	n.notify()
}

  go接口及嵌入类型例子

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-06
  • 2021-08-29
  • 2022-12-23
  • 2021-04-15
  • 2021-09-18
  • 2022-12-23
猜你喜欢
  • 2021-06-21
  • 2021-05-26
  • 2021-09-12
  • 2021-11-13
  • 2022-02-18
  • 2021-07-02
  • 2022-12-23
相关资源
相似解决方案