Skip to content

Instantly share code, notes, and snippets.

@cceckman
Last active December 14, 2017 07:55
Show Gist options
  • Save cceckman/9565083fcada559258394ba6ba85c686 to your computer and use it in GitHub Desktop.
Save cceckman/9565083fcada559258394ba6ba85c686 to your computer and use it in GitHub Desktop.
Embedding != inheritance. This is Go (not Java/Python/C++ with 'Foo' as a virtual method), so this outputs FooBar instead of CrowBar.
package main
import(
"fmt"
)
type FooBar struct {}
func (f *FooBar) Foo() string {
return "Foo"
}
func (f *FooBar) Bar() string {
return f.Foo() + "Bar"
}
type Crow struct {
*FooBar
}
func (c *Crow) Foo() string {
return "Crow"
}
func main() {
x := &Crow{
FooBar: &FooBar{},
}
// Outputs: FooBar
fmt.Printf(x.Bar())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment