How does embedding work Golang? Why is my struct not embedding the other?

Viewed 14

I'm new to Go and was learning about embedding and how inheritance does not work in Go like in other OOP languages. I have a basic struct called 'animal' that is embedded into another struct called 'bird'.

type animal struct {
        name string
        age  int
}

type bird struct {
        animal
        canFly      bool
        hasFeathers bool
}

newBird := bird{
        name:        "Parrot",
        age:         12,
        canFly:      true,
        hasFeathers: true,
}

This throws in error though and I'm not too sure why. My compiler says that "unknown field name in struct literal". Can anyone explain why this is?

0 Answers
Related