Last active
August 29, 2015 14:21
-
-
Save jaekwon/0f6e5555ab6a592aa4c8 to your computer and use it in GitHub Desktop.
Example of Go composition
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
// 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