Declaring Fields
// Sample program to show how what we are doing is NOT embedding
// a type but just using a type as a field.
//示例程序演示我们所做的不是嵌入的。
// a类型,只是使用类型作为字段。
package main
import "fmt"
// user defines a user in the program.
type user struct {
name string
email string
}
// notify implements a method notifies users 通知实现方法通知用户。
// of different events.
func (u *user) notify() {
fmt.Printf("Sending user email To %s<%s>
",
u.name,
u.email)
}
// admin represents an admin user with privileges. admin代表具有特权的admin用户。
type admin struct {
person user // NOT Embedding
level string
}
func main() {
// Create an admin user.
ad := admin{
person: user{
name: "admin1",
email: "admin1@qq.com",
},
level: "super",
}
// We can access fields methods.
ad.person.notify()
}
/*
Sending user email To admin1<admin1@qq.com>
*/
Embedding types
// Sample program to show how to embed a type into another type and
// the relationship between the inner and outer type.
//示例程序演示如何将类型嵌入到另一个类型中。
//内在与外在的关系。
package main
import "fmt"
// user defines a user in the program.
type user struct {
name string
email string
}
// notify implements a method notifies users
// of different events.
//通知实现方法通知用户。
//不同的事件。
func (u *user) notify() {
fmt.Printf("Sending user email To %s<%s>
",
u.name,
u.email)
}
// admin represents an admin user with privileges.
type admin struct {
user // Embedded Type 埋入式
level string
}
func main() {
// create an admin user
ad := admin{
user: user{
name: "tomcat",
email: "tomcat@aa.com",
},
level: "super",
}
// We can access the inner type's method directly. 我们可以直接访问内部类型的方法。
ad.user.notify()
// The inner type's method is promoted. 提升了内部类型的方法。
ad.notify()
}
Embedded types and interfaces
// Sample program to show how embedded types work with interfaces.
package main
import "fmt"
// notifier is an interface that defined notification
// type behavior.
// notifier是一个定义通知的接口。
// 类型的行为。
type notifier interface {
notify()
}
// user defines a user in the program.
type user struct {
name string
email string
}
// notify implements a method notifies users
// of different events.
func (u *user) notify() {
fmt.Printf("send user email to%s<%s>
",
u.name,
u.email)
}
// admin represents an admin user with privileges.
type admin struct {
user
level string
}
func main() {
// Create an admin user.
ad := admin{
user: user{
name: "john smith",
email: "john@yahoo.com",
},
level: "super",
}
// Send the admin user a notification.
// The embedded inner type's implementation of the
// interface is "promoted" to the outer type.
sendNotification(&ad)
// send user email tojohn smith<john@yahoo.com>
}
// sendNotification accepts values that implement the notifier
// interface and sends notifications.
// sendNotification接受实现通知的值。
// 接口和发送通知。
func sendNotification(n notifier) {
n.notify()
}
Outer and inner type interface implementations 外部和内部类型接口实现。
package main
import "fmt"
// notifier is an interface that defined notification
// type behavior.
type notifier interface {
notify()
}
// user defines a user in the program.
type user struct {
name string
email string
}
// notify implements a method notifies users
// of different events.
func (u *user) notify() {
fmt.Printf("Sending user email To %s<%s>
",
u.name,
u.email)
}
// admin represents an admin user with privileges.
type admin struct {
user
level string
}
// notify implements a method notifies admins
// of different events.
func (a *admin) notify() {
fmt.Printf("Sending admin Email To %s<%s>
",
a.name,
a.email)
}
func main() {
// Create an admin user.
ad := admin{
user: user{
name: "john smith",
email: "john@yahoo.com",
},
level: "super",
}
// Send the admin user a notification.
// The embedded inner type's implementation of the
// interface is NOT "promoted" to the outer type.
//发送管理用户通知。
//嵌入式内部类型的实现。
//界面不是“提升”到外部类型。
sendNotification(&ad)
// We can access the inner type's method directly. 我们可以直接访问内部类型的方法。
ad.user.notify()
// The inner type's method is NOT promoted. 内部类型的方法没有被提升。
ad.notify()
}
// sendNotification accepts values that implement the notifier
// interface and sends notifications.
func sendNotification(n notifier) {
n.notify()
}
/*
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>
*/
练习
// Copy the code from the template. Declare a new type called hockey
// which embeds the sports type. Implement the finder interface for hockey.
// When implementing the find method for hockey, call into the find method
// for the embedded sport type to check the embedded fields first. Then create
// two hockey values inside the slice of finders and perform the search.
//复制模板中的代码。宣布一种新型的曲棍球。
//这是体育运动的类型。实现曲棍球的finder接口。
//在实现曲棍球的查找方法时,请调用find方法。
//对于嵌入式运动类型,首先要检查嵌入式字段。然后创建
//两个曲棍球值,在切片的发现者和执行搜索。
package main
import (
"fmt"
"strings"
)
// finder defines the behavior required for performing matching. finder定义了执行匹配所需的行为。
type finder interface {
find(needle string) bool
}
// sport represents a sports team. 体育代表队。
type sport struct {
team string
city string
}
// find checks the value for the specified term. 查找检查指定项的值。
func (s *sport) find(needle string) bool {
return strings.Contains(s.team, needle) || strings.Contains(s.city, needle)
}
// hockey represents specific hockey information. 曲棍球代表特定的曲棍球信息。
type hockey struct {
sport
country string
}
// find checks the value for the specified term. 查找检查指定项的值。
func (h *hockey) find(needle string) bool {
return h.sport.find(needle) || strings.Contains(h.country, needle)
}
func main() {
// Define the term to find.
needle := "Miami"
// Create a slice of finder values and assign values
// of the concrete hockey type.
haystack := []finder{
&hockey{sport{"Panthers", "Miami"}, "USA"},
&hockey{sport{"Canadiens", "Montreal"}, "Canada"},
}
// Display what we are trying to find.
fmt.Println("Searching For:", needle)
// Range of each haystack value and check the term.
for _, hs := range haystack {
if hs.find(needle) {
fmt.Printf("FOUND: %+v", hs)
}
}
}
/*
Searching For: Miami
FOUND: &{sport:{team:Panthers city:Miami} country:USA}
*/