Skip to content

Instantly share code, notes, and snippets.

@calderonroberto
Last active November 30, 2015 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calderonroberto/660c2aaff89ac271599b to your computer and use it in GitHub Desktop.
Save calderonroberto/660c2aaff89ac271599b to your computer and use it in GitHub Desktop.
Composing in GoLang
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