A Complete Guide to Composition in GO

Composition is a fundamental concept in Go that allows developers to build complex systems by combining simpler, reusable components. Unlike inheritance in other languages, Go promotes composition to achieve code reuse and modularity. This blog will explore what composition is, how to use it in Go, and some patterns and best practices for leveraging it effectively.

What is Composition?

Composition is a design principle where a class (or in Go's case, a struct) is composed of one or more other classes or structs. Instead of inheriting properties and behaviors from a parent class, a struct can embed other structs and delegate tasks to them. This approach fosters flexibility and reusability while avoiding the pitfalls of inheritance hierarchies.

Composition in Go

In Go, composition is typically achieved through struct embedding. Struct embedding allows one struct to include another struct, effectively giving it all the methods and fields of the embedded struct. This approach can be used to model "has-a" relationships between types.

Basic Example

Here's a simple example demonstrating struct composition:

package main

import "fmt"

// Engine struct represents an engine.
type Engine struct {
    Power int
}

// Start method simulates starting the engine.
func (e Engine) Start() {
    fmt.Println("Engine started with power:", e.Power)
}

// Car struct represents a car that has an Engine.
type Car struct {
    Engine // Embedding Engine struct
    Model  string
}

func main() {
    myCar := Car{
        Engine: Engine{Power: 150},
        Model:  "Sedan",
    }

    myCar.Start() // Calls Start method from Engine
    fmt.Println("Car model:", myCar.Model)
}

Explanation:

  • Engine is a struct with a method Start.

  • Car embeds Engine, gaining access to Engine's methods and fields.

  • The Start method of Engine can be called on Car, demonstrating composition.

Use Cases

  1. Code Reuse: Composition allows for the reuse of existing code without inheritance. You can build complex types by combining simpler, reusable components.

  2. Flexibility: Changing an embedded struct doesn’t require changes in the embedding struct's interface, making your code more flexible and easier to maintain.

  3. Separation of Concerns: You can separate different concerns or responsibilities into distinct structs, improving code organization and readability.

Patterns and Best Practices

  1. Interface Composition: When designing your system, you can use interfaces to define the behavior your structs should implement. For example:

     package main
    
     import "fmt"
    
     // Speaker interface defines a speak behavior.
     type Speaker interface {
         Speak() string
     }
    
     // Person struct represents a person who can speak.
     type Person struct {
         Name string
     }
    
     func (p Person) Speak() string {
         return "Hello, my name is " + p.Name
     }
    
     // Robot struct represents a robot that can speak.
     type Robot struct {
         ID string
     }
    
     func (r Robot) Speak() string {
         return "Beep boop, ID: " + r.ID
     }
    
     func main() {
         var speaker Speaker
    
         speaker = Person{Name: "Alice"}
         fmt.Println(speaker.Speak())
    
         speaker = Robot{ID: "R2D2"}
         fmt.Println(speaker.Speak())
     }
    

    Explanation: The Speaker interface is composed of the Speak method. Both Person and Robot structs implement this interface, allowing polymorphic behavior.

  2. Delegation: When using composition, you can delegate responsibilities to the embedded structs:

     package main
    
     import "fmt"
    
     // Printer struct is a utility for printing.
     type Printer struct{}
    
     func (p Printer) Print(message string) {
         fmt.Println(message)
     }
    
     // Document struct represents a document with printing capabilities.
     type Document struct {
         Printer // Composition
         Title   string
     }
    
     func main() {
         doc := Document{
             Title: "My Document",
         }
    
         doc.Print("Printing document: " + doc.Title)
     }
    

    Explanation: Document embeds Printer and delegates the printing task to it.

  3. Combining Behaviors: Use composition to combine multiple behaviors:

     package main
    
     import "fmt"
    
     // Drivable interface defines a drive behavior.
     type Drivable interface {
         Drive() string
     }
    
     // Flyable interface defines a fly behavior.
     type Flyable interface {
         Fly() string
     }
    
     // FlyingCar struct combines driving and flying capabilities.
     type FlyingCar struct {
         Make  string
         Model string
     }
    
     func (f FlyingCar) Drive() string {
         return "Driving the car: " + f.Make + " " + f.Model
     }
    
     func (f FlyingCar) Fly() string {
         return "Flying the car: " + f.Make + " " + f.Model
     }
    
     func main() {
         myFlyingCar := FlyingCar{Make: "Flyer", Model: "X1"}
    
         fmt.Println(myFlyingCar.Drive())
         fmt.Println(myFlyingCar.Fly())
     }
    

    Explanation: FlyingCar combines Drivable and Flyable behaviors.

Summary

Composition in Go is a powerful technique that promotes code reuse, modularity, and flexibility. By embedding structs and using interfaces, you can build complex systems from simpler components, fostering maintainable and extensible code.

Key Takeaways:

  • Struct Embedding: Use embedding to gain access to methods and fields of another struct.

  • Interface Composition: Use interfaces to define and combine behaviors.

  • Delegation: Delegate responsibilities to embedded structs to achieve separation of concerns.

By understanding and applying composition effectively, you can design robust and scalable Go applications.

0
Subscribe to my newsletter

Read articles from Sundaram Kumar Jha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sundaram Kumar Jha
Sundaram Kumar Jha

I Like Building Cloud Native Stuff , the microservices, backends, distributed systemsand cloud native tools using Golang