Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.