Instantly share code, notes, and snippets.

Embed
What would you like to do?
composition example
# https://www.youtube.com/watch?v=gRpUfjTwSOo&utm_source=golangweekly&utm_medium=email
package main
import "fmt"
type user struct {
name string
email string
}
type admin struct {
user
level string
}
func(u *user) notify() {
fmt.Printf("Sending email to %s<%s>\n",
u.name,
u.email,
)
}
type notifier interface {
notify()
}
func main() {
foo := user{
name: "Randolf Gorgeous",
email: "fake@also.com",
}
admin := admin{
user: user{
name: "mark platt",
email: "fake@fake.com",
},
level: "super",
}
sendNotification(&admin)
sendNotification(&foo)
}
func sendNotification(n notifier) {
n.notify()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment