Composition with Structs
Learn about composition in Go and how to achieve it through struct embedding.
We'll cover the following
Before we test complex scenarios, we have to learn more about structs and composition. These two concepts are mandatory to implement the dependency injection pattern and write loosely-coupled software.
Inheritance vs. composition
Go doesn’t support inheritance. Inheritance can be defined as a hierarchical relationship between parent and child classes. In practice, child classes inherit the fields, properties, and methods of parent classes. In Go, we don’t have classes, so this concept can’t be applied. Instead of classes, we have structs, and in place of inheritance, we have composition.
Let’s visualize the difference between these two terms:
Get hands-on with 1200+ tech skills courses.