Last active
November 30, 2015 23:31
-
-
Save calderonroberto/660c2aaff89ac271599b to your computer and use it in GitHub Desktop.
Composing in GoLang
This file contains 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
package main | |
import ( | |
"fmt" | |
) | |
type User struct { | |
Name string | |
} | |
func (u *User) sayName(speaker string) bool { | |
fmt.Printf("Hello %s, my name is %s\n", speaker, u.Name) | |
return true | |
} | |
type Role interface { | |
unlockGate() bool | |
} | |
type Admin struct { | |
*User | |
roleType string | |
SecretPassword string | |
entries int32 | |
} | |
// A method needed by the Role interface. | |
func (a *Admin) unlockGate() bool { | |
a.entries++ | |
fmt.Printf("unlocking gate as %s\n", a.Name) | |
return true | |
} | |
// A function that works on any Role. | |
func unlock(r Role) { | |
r.unlockGate() | |
} | |
func main() { | |
b := &Admin{&User{"Pancho"}, "administrator", "pa$$word", 0} | |
b.sayName("Roberto") | |
b.unlockGate() | |
unlock(b) | |
fmt.Printf("Total entries %d\n", b.entries) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment