forked from hoanhan101/ultimate-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
embedding_1.go
42 lines (35 loc) · 983 Bytes
/
embedding_1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// -------------------------------
// Declaring Fields, NOT embedding
// -------------------------------
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>\n", u.name, u.email)
}
// admin represents an admin user with privileges.
// person user is not embedding. All we do here just create a person field based on that other
// concrete type named user.
type admin struct {
person user // NOT Embedding
level string
}
func main() {
// Create an admin user using struct literal.
// Since person also has struct type, we use another literal to initialize it.
ad := admin{
person: user{
name: "Hoanh An",
email: "hoanhan@bennington.edu",
},
level: "superuser",
}
// We call notify through the person field through the admin type value.
ad.person.notify()
}