I'm having some issues understanding Go's struct inheritance. I'm trying to do somewhat of an abstraction for an object type. See the example code below:
package main
type Animal struct{}
type Dog struct {
Animal
Color string
}
type Person struct {
Name string
Age int
Pet *Animal
}
func main() {
dog := &Dog{Color: "brown"}
tom := &Person{Name: "Tom", Age: 13, Pet: dog}
}
This is causing a compilation error:
cannot use dog (type *Dog) as type *Animal in field value
What is the correct way to go about doing an abstraction like this? Is it possible in Go?
End goal for the example would be to have different types of Pets - Dog, Cat, Hamser, etc. Then be able to store that into a struct expecting type Animal.
To visualize, something like:
type Person struct {
Name string
Age int
Pet *Dog OR *Cat OR *Hamster
}