Go
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions and Methods
-
Section 4: Concurrency
-
Section 5: Packages and Tooling
-
Section 6: More Standard Library
-
Section 7: Building Services
-
Section 8: Advanced Go
-
Section 9: Go in the Cloud-Native Ecosystem
-
Section 10: Data Structures and Algorithms in Go
-
Section 11: Testing and Deployment
-
Section 12: Practical Projects
-
Section 13: More Standard Library Practice
-
Section 14: More Practice Projects
-
Section 15: Design Patterns in Go
-
Section 16: Interview Practice
-
Section 17: Package fmt In Depth
-
Section 18: Package strings and strconv
-
Section 19: Package os and io
-
Section 20: Package time
-
Section 21: Package sort and container
-
Section 22: Package encoding
-
Section 23: Package net/http In Depth
-
Section 24: Package context
-
Section 25: Package regexp and bytes
-
Section 26: Package errors In Depth
-
Section 27: Package crypto and hash
-
Section 28: Package flag and log
-
Section 29: Package sync In Depth
-
Section 30: More Practice Exercises
-
Section 31: Go Modules and Workspaces In Depth
-
Section 32: Generics Deep Dive (Go 1.18+)
-
Section 33: Testing Package In Depth
-
Section 34: More Interview and Whiteboard Practice
-
Section 35: Package math and unicode
-
Section 36: Package path and filepath
-
Section 37: Package database/sql
-
Section 38: Package text/template and html/template
-
Section 39: Package archive and compress
-
Section 40: Lower-Level net Package
-
Section 41: Package runtime and reflect
-
Section 42: Package embed and io/fs
-
Section 43: Go Assembly and CGO Basics
-
Section 44: Building CLIs and TUIs
-
Section 45: Go Performance Tuning
-
Section 46: More Real-World Projects
-
Section 47: Go in Production
-
Section 48: Go Design Patterns
-
Section 49: Go Interfaces Deep Dive
-
Section 50: Final Practice and Review
238: Interface Composition
Think about a job posting for a "Senior Executive Assistant." The job description doesn't usually list every single single human skill from scratch. Instead, it might say, "Must be an expert Calendar Manager AND an expert Travel Coordinator." The role of the Assistant is essentially a composition of two other, smaller roles. If you can handle the calendar and you can handle the travel, you satisfy the requirements for the Assistant role.
In Go, interface composition works exactly like that. Instead of defining a massive interface with fifteen methods, you define small, focused interfaces and then embed them into a larger one. You're essentially saying, "To satisfy this new interface, you must satisfy all of these other interfaces first."
Combining Capabilities
Let's look at a real-world scenario: a file system. You might have some objects that can only be read from, and some that can only be written to. Rather than creating one giant File interface and forcing every single object to implement every method, we break them apart.
type Reader interface {
Read(p []byte) (n int, err error)
}
type Writer interface {
Write(p []byte) (n int, err error)
}
// ReadWriter is a composed interface.
// It embeds both Reader and Writer.
type ReadWriter interface {
Reader
Writer
}
Notice that ReadWriter doesn't have any methods of its own. It just "borrows" the requirements from the other two. If I write a function that takes a ReadWriter, I can be 100% certain that whatever object I'm handed has both a Read and a Write method. It's a clean way to build complex requirements out of simple building blocks.
Why bother with this instead of one big interface?
I've seen a lot of developers start by creating "God Interfaces"—interfaces with twenty methods that try to cover every single possibility. The problem is that your code becomes incredibly rigid. If you have a Database interface with Connect(), Query(), and Shutdown(), but you have a mock object that only needs to simulate Query(), you're still forced to implement Connect and Shutdown just to make the compiler happy.
By using composition, you keep your interfaces "lean." You can write a function that only requires a Reader, and it will work with a ReadWriter, a File, or a NetworkSocket. The more you decompose your interfaces, the more reusable your functions become. It's the "secret sauce" that makes Go's type system feel so flexible despite being statically typed.
Adding Specifics to the Mix
Composition isn't limited to just embedding other interfaces. You can embed multiple interfaces and still add your own unique methods to the composed one. It's not an "either-or" situation.
type Closer interface {
Close() error
}
// ReadWriteCloser is a powerhouse interface.
// It requires Read, Write, and Close.
type ReadWriteCloser interface {
Reader
Writer
Closer
Flush() error // A specific method just for this composed interface
}
Now, any type that wants to be a ReadWriteCloser must implement four methods. But the beauty is that Reader, Writer, and Closer remain independent. You can still use them individually throughout your app without dragging the Flush requirement along for the ride.
📋 Practical Task
Implementing a Smart Home Controller
You are building a system to manage smart home devices. Some devices are just Powerable (they can turn on and off), some are Dimmable (they can change brightness), and some are both.
Your task is to:
- Define a
Powerableinterface withTurnOn()andTurnOff()methods. - Define a
Dimmableinterface with aSetBrightness(level int)method. - Create a composed interface called
SmartLightthat embeds bothPowerableandDimmable, and adds its own method:SetColor(color string). - Implement a struct called
HueBulbthat satisfies theSmartLightinterface. - Write a function called
InitializeDevicethat accepts aSmartLightand calls all four methods on it to set a default state.
There are no comments for now.