Composition vs. Inheritance
Composition and inheritance are two ways to achieve code reuse and build complex systems. Inheritance is a mechanism that allows a class to inherit properties and behavior from a parent class. Composition is a way of building complex objects by combining smaller, simpler objects.
In Go, composition is favored over inheritance. This is because Go does not have classes like traditional object-oriented programming languages. Instead, Go uses structs to define objects and interfaces to define behavior. Composition is achieved by embedding one struct into another.
For example, a solution to compose a simple report in Go can be model as follows:
type celsius float64type temperature struct {
maximum, minimum celsius
}type location struct {
latitude, longitude float64
}type report struct {
sol int
temperature temperature
location location
}