I'm experimenting with GoLang and interfaces and struct inheritence.
I've created a set of structures with the idea that I can keep common methods and values in a core structure an then just inherit this and add extra values as appropriate:
type NamedThing interface {
GetName() string
GetAge() int
SetAge(age int)
}
type BaseThing struct {
name string
age int
}
func (t BaseThing) GetName() string {
return t.name
}
func (t BaseThing) GetAge() int {
return t.age
}
func (t BaseThing) SetAge(age int) {
t.age = age
}
type Person struct {
BaseThing
}
func main() {
p := Person{}
p.BaseThing.name = "fred"
p.BaseThing.age = 21
fmt.Println(p)
p.SetAge(35)
fmt.Println(p)
}
Which you can also find here in the go playground:
https://play.golang.org/p/OxzuaQkafj
However when I run the main method, the age remains as "21" and isn't updated by the SetAge() method.
I'm trying to understand why this is and what I'd need to do to make SetAge work correctly.