Lesson 3
Composition in Go
Composition in Go

Welcome back! Having explored structs and interfaces, you are now ready to dive into the concept of composition in Go. This lesson will show you how to build complex and modular types by composing existing ones. Composition is a powerful feature that lets you extend the functionality of your structs organically, making your code cleaner and more maintainable.

What You'll Learn

In this lesson, we will:

  1. Understand the basics of composition in Go.
  2. See how composition differs from inheritance.
  3. Learn to create and use composite types.
Example: Creating an Employee Struct

We will use an example to illustrate composition. Let's start by defining a Person struct and then create an Employee struct that includes Person as an embedded struct.

Here is our code snippet:

Go
1package main 2 3import "fmt" 4 5// Define a basic struct for a Person 6type Person struct { 7 Name string 8 Age int 9} 10 11// Define a struct for an Employee that includes a Person 12type Employee struct { 13 Person // Embedded struct (Composition) 14 EmployeeID string 15 Position string 16} 17 18func main() { 19 // Create a new Employee 20 emp := Employee{ 21 Person: Person{ 22 Name: "John Doe", 23 Age: 30, 24 }, 25 EmployeeID: "E12345", 26 Position: "Software Engineer", 27 } 28 29 // Accessing fields of the embedded Person struct directly from Employee 30 fmt.Println("Name:", emp.Name) // Output: Name: John Doe 31 fmt.Println("Age:", emp.Age) // Output: Age: 30 32 fmt.Println("Employee ID:", emp.EmployeeID) // Output: Employee ID: E12345 33 fmt.Println("Position:", emp.Position) // Output: Position: Software Engineer 34}

In this code, Employee is composed of the Person struct. You can directly access the Name and Age fields from emp because Person is embedded in Employee.

Note, that in Go, when you embed a struct within another struct, you don't need to specify a field name for the embedded struct. This is known as anonymous fields or embedding. This allows you to access the fields of the embedded struct directly from the outer struct.

Why It Matters

Mastering composition is essential to building scalable and maintainable software. Unlike inheritance, which can lead to tightly coupled code and hierarchical complexity, composition promotes loose coupling and code reusability.

By using composition, you can:

  • Share code and functionality between different structs.
  • Avoid deep inheritance hierarchies.
  • Create more flexible and idiomatic Go code.

Understanding and using composition effectively will improve your ability to design robust systems and make your coding practice more efficient.

Are you ready to put these concepts into practice? Excellent! Let’s proceed to the practice section, where you’ll get hands-on experience with composition in Go.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.