Composition

Composition implements a "has-a" relationship. It allows a class to contain an object of another class.











Advantages

  • Flexibility:

    Composition and interfaces allow for decoupled designs. You can change the components of a struct or the implementation of an interface without affecting other parts of your program.










  • Simplicity:

    Avoiding classical inheritance can lead to simpler, more straightforward code. It encourages thinking in terms of what objects do (behaviors) rather than what they are in an inheritance hierarchy.










  • Maintainability:

    Code that uses interfaces and composition is often easier to maintain and extend, as changes tend to be more localized.


Source: https://medium.com/@chaewonkong/go-composition-and-interfaces-a4b4346f8565