First, I'm quite new to Go, so please forgive any lapses or errors in terminology. I suspect my lack of command with the terminology is also partially to blame for not finding an answer to the following question after many hours of looking.
In short, I would like the output of the following code to be
I am the Adult
I am the Child
Where instead the output is
I am the Adult
I am the Adult
Code:
package main
import "fmt"
type Human struct {
age uint
name string
}
func (h Human) sayName() error {
fmt.Println("I am the Adult")
return nil
}
func (h Human) Introduce() error {
h.sayName()
return nil
}
type Child struct {
Human
}
func (c Child) sayName() error {
fmt.Println("I am the Child")
return nil
}
func main() {
h := Human{}
h.Introduce()
c := Child{Human{}}
c.Introduce()
}
So in essence, though Introduce() is only implemented in the embedded type, Human, it calls sayName(), which is implemented in both the embedded type and the embedding type.
I understand that the current output is the way as it is because the embedded Human struct doesn't "know" it's embedded and thus could never call Child.sayName and would only call its own sayName() function.
Is there a way to instantiate a Human struct (or a struct embedding one) in which you can "replace" Human.sayName() with an alternate sayName() function?