-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathembedding.go
49 lines (41 loc) · 1.14 KB
/
embedding.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
43
44
45
46
47
48
49
// Create a user type, and an admin type that embeds a user. Create a notifier
// interface, and make your user type satisfy that interface. Write a function
// that accepts a value of the interface type, and ensure it works correctly
// when passed a value of your admin type.
package main
// Add your imports here.
import "fmt"
// Define a `notifier` interface.
type notifier interface {
notify()
}
// Create a `user` type, with fields for name and email address. Ensure your
// type satisfies the Notifier interface.
type user struct {
name, email string
}
func (u user) notify() {
fmt.Printf("Sending email to %s at %s\n", u.name, u.email)
}
// Create an `admin` type which embeds a user, and has a security level.
type admin struct {
user
level int
}
// Write a function that accepts a value of your interface and calls the method
// associated with that interface.
func sendNotification(n notifier) {
n.notify()
}
func main() {
// Create an admin user.
user := admin{
user: user{
name: "Tim Blair",
email: "[email protected]",
},
level: 10,
}
// Send the admin a notification via the function you created.
sendNotification(user)
}