0 votes
in GoLang by
How do we perform inheritance with Golang?

1 Answer

0 votes
by

using composition to use an existing struct object to define a starting behavior of a new object. Once the new object is created, functionality can be extended beyond the original struct.

type Animal struct {

    // …

}

func (a *Animal) Eat()   { … }

func (a *Animal) Sleep() { … }

func (a *Animal) Run() { … }

type Dog struct {

    Animal

    // …

}

The Animal struct contains Eat(), Sleep(), and Run() functions. These functions are embedded into the child struct Dog by simply listing the struct at the top of the implementation of Dog.

Related questions

0 votes
asked Aug 11, 2022 in GoLang by SakshiSharma
0 votes
asked Aug 11, 2022 in GoLang by SakshiSharma
...