I am trying understanding Google Go's embedding mechanism (as an alternative to subclassing). Below is a simple program that summarizes my problem with the approach:
package main
import "fmt"
type Person struct {
Name string
}
func (p *Person) Talk() {
fmt.Println("Hi, my name is Person")
}
func (p *Person) TalkVia() {
fmt.Println("TalkVia ->")
p.Talk()
}
type Android struct {
Person
}
func (p *Android) Talk() {
fmt.Println("Hi, my name is Android")
}
func main() {
fmt.Println("Person")
p := new(Person)
p.Talk()
p.TalkVia()
fmt.Println("Android")
a := new(Android)
a.Talk()
a.TalkVia()
}
The output is:
Person
Hi, my name is Person
TalkVia ->
Hi, my name is Person
Android
Hi, my name is Android
TalkVia ->
Hi, my name is Person
but if I was subclassing (in another language), the output will be:
Person
Hi, my name is Person
TalkVia ->
Hi, my name is Person
Android
Hi, my name is Android
TalkVia ->
Hi, my name is Android
Is there a way to achieve this last output with google go embedding (not interfaces)?