Skip to content

Instantly share code, notes, and snippets.

@codemartial
Last active December 22, 2015 04:58
Show Gist options
  • Save codemartial/6420383 to your computer and use it in GitHub Desktop.
Save codemartial/6420383 to your computer and use it in GitHub Desktop.
A demo of how embedding in Go does not result in polymorphic behaviour, unlike inheritance. More here: http://tech.t9i.in/2014/01/inheritance-semantics-in-go/
package main
import (
"fmt"
)
type Foo struct {
}
type Bar struct {
Foo
}
func (foo Foo) a() {
fmt.Println("Foo a() calling b()")
foo.b()
}
func (foo Foo) b() {
fmt.Println("Foo b() called")
}
func (bar Bar) b() {
fmt.Println("Bar b() called")
}
func main() {
bar := Bar{}
// Even though bar has an override for b(), it won't be called
bar.a()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment