Skip to content

Instantly share code, notes, and snippets.

@jaekwon
Last active August 29, 2015 14:21
Show Gist options
  • Save jaekwon/0f6e5555ab6a592aa4c8 to your computer and use it in GitHub Desktop.
Save jaekwon/0f6e5555ab6a592aa4c8 to your computer and use it in GitHub Desktop.
Example of Go composition
// Compare to https://gist.github.com/jaekwon/8025b9f3a482b3219a21
package main
import "fmt"
type Human struct{}
func (h Human) walk() { h.moveFeet() }
func (h Human) moveFeet() { fmt.Println("Human.walkFeet") }
type Dork struct {
Human // embedded struct, so Human methods are exposed on Dork.
}
func (d Dork) moveFeet() { fmt.Println("Dork.walkFeet") }
func main() {
d := Dork{}
d.walk() // Human.walkFeet
d.moveFeet() // Dork.walkFeet
d.Human.moveFeet() // Human.walkFeet
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment