Created
March 22, 2015 13:34
-
-
Save mrkplt/b114d2525a03cfb95acd to your computer and use it in GitHub Desktop.
composition example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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: "[email protected]", | |
| } | |
| admin := admin{ | |
| user: user{ | |
| name: "mark platt", | |
| email: "[email protected]", | |
| }, | |
| 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