Idiomatic Go Patterns
Design Patterns & Best Practices in Go
Real Go implementations of the full Gang of Four catalog, written around small interfaces, composition, concurrency safety, and explicit errors.
Pattern Categories
Go still uses creational, structural, and behavioral patterns, but it adapts them through interfaces and composition instead of inheritance-heavy class hierarchies.
Creational
5 patternsPatterns for object creation in Go, usually expressed through constructor functions, interfaces, and functional options.
Explore patternsStructural
7 patternsPatterns for composing types and boundaries in Go with interfaces, embedding, adapters, and focused packages.
Explore patternsBehavioral
10 patternsPatterns for organizing control flow, workflows, and collaboration with explicit errors, interfaces, and small units of behavior.
Explore patternsPublished Go Articles
Each article includes working Go source, Go-specific tradeoffs, and guidance on when the pattern helps or hurts.
Abstract Factory in Go
Create related objects through one factory so whole product families can change together without leaking concrete types.
Builder in Go
Separate a multi-step construction workflow from the final representation so the same input can produce different outputs cleanly.
Factory Method in Go
Use constructor functions and small interfaces to select concrete implementations without coupling callers to them.
Prototype Pattern in Go
Clone preconfigured object graphs when copying is simpler or faster than rebuilding the same structure from scratch.
Singleton Pattern in Go
Use sync.Once carefully for one-time initialization, and understand when dependency injection is a better fit.
Adapter in Go
Wrap an incompatible API behind the interface your application already expects so the rest of the code stays unchanged.
Bridge in Go
Separate an abstraction from its implementation so both sides can vary independently without multiplying concrete types.
Composite in Go
Treat individual objects and object groups uniformly so recursive tree structures stay simple to traverse and aggregate.
Decorator in Go
Add behavior around an object dynamically by wrapping it with small focused layers that share the same interface.
Facade in Go
Provide one simplified entry point over several collaborating subsystems so callers can trigger a workflow without knowing every step.
Flyweight in Go
Share intrinsic immutable state across many small objects so large collections stay cheaper in memory and easier to manage.
Proxy in Go
Control access to another object by standing in front of it to add caching, authorization, rate limiting, or lazy loading.
Chain of Responsibility in Go
Build validation and request-processing pipelines with small handlers composed through interfaces.
Command Pattern in Go
Encapsulate operations for queuing, audit history, and undo/redo while keeping Go error handling explicit.
Iterator in Go
Traverse a collection without exposing its internal representation, keeping traversal state separate from the aggregate.
Mediator in Go
Centralize communication between collaborating objects so they depend on a coordinator instead of referencing each other directly.
Memento in Go
Capture and restore object state without exposing internal details so undo and rollback remain explicit and controlled.
Observer in Go
Notify dependent subscribers when state changes so event-driven reactions stay decoupled from the publisher.
State in Go
Change an object’s behavior when its internal state changes by delegating transitions and rules to state-specific types.
Strategy in Go
Encapsulate interchangeable algorithms behind one interface so callers can swap behavior without branching on concrete rules.
Template Method in Go
Define the skeleton of an algorithm once while letting concrete steps vary through narrow hook methods.
Visitor in Go
Add operations across a stable object structure without changing each node type whenever a new operation is introduced.