Last active
December 22, 2015 04:58
-
-
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/
This file contains 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
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