Introduction

Welcome back! In this final lesson of our Go Structs Basics Revision course, we will explore how Go promotes code reuse and flexibility through composition and methods. While some languages rely heavily on inheritance, Go prefers composition to share functionality between types, enhancing code readability and maintainability.

In this lesson, we'll explore how Go achieves shared behavior with composition by embedding structs and using interfaces. Our lesson plan includes understanding composition, leveraging embedded structs for shared attributes, utilizing methods to capture behavior, and learning Go-specific idioms for initializing structs. Ready? Let's dive in!

Defining Composition

In Go, composition is favored over classical inheritance. Instead of inheriting from a base type, Go types can include or "embed" other types to reuse their functionality.

Here's an example using a struct named Vehicle and another struct named Car:

In this example, Car includes Vehicle as an embedded field. This allows Car to directly access Vehicle's fields (Color and Brand) without having to qualify them with the embedded struct's name (i.e., myCar.Vehicle.Color). This demonstrates one way Go facilitates attribute reuse using composition.

Attribute Inheritance

In Go, we achieve shared attributes through embedded structs, which allow one struct to include fields from another.

Consider this example using a struct named Artist and another struct named Musician:

The Musician struct includes Artist as an embedded struct, allowing Musician to access fields originating from Artist (in this case, Name). The Display method illustrates how this struct pattern can express collective information using attributes from both the embedded and top-level structs.

Method Inheritance

In Go, methods are associated with receiver types, and embedded structs allow types to expose methods from other structs.

Here's an example where Vehicle is a struct with a Start method, and Car embeds Vehicle to reuse this method:

By embedding Vehicle, Car can use Vehicle's Start method without having to redefine it. This showcases how Go's composition model effectively allows for method reuse, providing inherited behavior more naturally compared to traditional inheritance.

Idiomatic Struct Initialization

Instead of constructors, Go promotes using struct literals and factory functions to initialize structs. This provides flexibility and readability:

Go encourages initializing data directly or through helper functions, like NewProduct, for cleaner and more maintainable code. These factory functions can encapsulate any complex initialization logic, making the code using Product easy to read and understand.

Lesson Summary

We've explored how Go handles attribute and method sharing using composition and interfaces. By embedding structs and utilizing methods effectively, Go ensures efficient code reuse without the complexity of inheritance. Practicing these concepts in Go will enhance your understanding and ability to write clean, maintainable code. Ready to try some exercises? Remember, programming is about exploring and problem-solving. Enjoy the journey!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal