Is there a way to access the name of a "Child" struct from methods on the "Parent" struct when using anonymous method embedding.
For Example:
type Animal struct{}
func (a Animal) SayName() string {
v := reflect.TypeOf(a)
return v.Name()
}
type Zebra struct {
Animal
}
var zebra Zebra
zebraName := zebra.SayName() // "Animal" want "Zebra"
The SayName() method returns the type.Name()
of the "Parent".
I realize I could do something like this, but since this for an API and will be reused often. I would prefer to have a solution that is less repetitive.
type Animal struct{
Name string
}
func (a Animal) SayName() string {
return a.Name
}
type Zebra struct {
Animal
}
zebra := &Zebra{Name:"Zebra"}
zebraName := zebra.SayName() // "Zebra"
Any ideas on how this could be accomplished? Is this possible in Go?
Thank you.