A comprehensive, practical, and production-ready collection of Go design patterns
Learn • Reference • Practice
This isn't just another list of Go patterns. It's a hands-on, production-aware collection where every pattern comes with:
| Feature | Description |
|---|---|
| ✅ Runable Code | Every pattern has a main.go you can execute immediately |
| 📖 Detailed README | Each directory explains when/why/how to use the pattern |
| 🎯 Go-Specific | Patterns adapted to Go idioms, not Java/C++ translations |
| 🔧 Practical Examples | Real-world scenarios, not just theoretical Animal/Shape examples |
| 📊 Multiple Categories | From classic Gang of Four to modern Go concurrency patterns |
🏗️ Creational Patterns — Object creation mechanisms
- Singleton — Ensure a type has only one instance
- Factory Method — Delegate object creation to subclasses
- Abstract Factory — Create families of related objects
- Builder — Construct complex objects step by step
- Prototype — Clone objects instead of creating from scratch
🔌 Structural Patterns — Object composition and relationships
- Adapter — Make incompatible interfaces compatible
- Bridge — Separate abstraction from implementation
- Composite — Treat individual and composite objects uniformly
- Decorator — Add behavior without inheritance
- Facade — Simplify complex subsystems
- Flyweight — Share objects to support large quantities
- Proxy — Control access to another object
🔄 Behavioral Patterns — Object communication and responsibility
- Chain of Responsibility — Pass requests along a chain
- Command — Encapsulate requests as objects
- Interpreter — Define a grammar and interpret sentences
- Iterator — Traverse collections without exposing internals
- Mediator — Reduce coupling between objects
- Memento — Capture and restore object state
- Observer — Notify dependents of state changes
- State — Alter behavior when state changes
- Strategy — Select algorithms at runtime
- Template Method — Define skeleton of an algorithm
- Visitor — Separate algorithms from objects
⚡ Concurrency Patterns — Go's superpower! Goroutines & channels
- Goroutine per Task — Simple concurrent execution
- Worker Pool — Control concurrent goroutines
- Fan-In — Multiplex multiple channels into one
- Fan-Out — Distribute work across goroutines
- Pipeline — Stage-wise data processing
- Future/Promise — Async result handling
- Barrier — Synchronize multiple goroutines
- Mutex — Mutual exclusion
- Semaphore — Limit concurrent access
- RWMutex — Reader-writer locks
- Atomic — Lock-free operations
- Context Cancellation — Graceful cancellation
🎯 Go-Specific Patterns — Idiomatic Go practices
- Functional Options — Clean, extensible constructors
- Interface Segregation — Small, focused interfaces
- Dependency Injection — Constructor-based DI
- Error Wrapping — Contextual error handling (Go 1.13+)
- Sentinel Errors — Predefined error variables
- Error Type Assertion — Custom error types
- Empty Interface —
interface{}andanyusage - Type Assertion — Safe type conversion
- Embedding — Composition over inheritance
- Table-Driven Tests — Clean, maintainable tests
- Graceful Shutdown — Clean server termination
- Configuration — Struct tags and config loading
🏛️ Architectural Patterns — System-level organization
- MVC — Model-View-Controller
- MVP — Model-View-Presenter
- MVVM — Model-View-ViewModel
- Clean Architecture — Uncle Bob's architecture
- Hexagonal Architecture — Ports & Adapters
- Layered Architecture — N-tier architecture
- Repository Pattern — Data access abstraction
- Service Layer — Business logic boundary
- CQRS — Command Query Responsibility Segregation
- Event-Driven Architecture — Event-based communication
# Clone the repository
git clone https://github.com/dll-as/go-patterns.git
cd go-patterns
# Pick a pattern and run it!
cd Creational/singleton
go run main.go
# Or run tests if available
cd Go-Specific/table-driven-tests
go test -v-
Start with Creational patterns — they're the most intuitive. Then move to Structural and Behavioral. Each pattern's README explains the "what" and "why" before the "how".
-
Jump straight to Concurrency and Go-Specific patterns. These show you how Go differs from other launguages and how to write idiomatic Go code.
-
The Architectural patterns section demonstrates how to structure entire applications. See how clean architecture and hexagonal architecture are implemented in Go.
-
Each pattern directory contains:
pattern-name/ ├── main.go # Runnable, self-contained example ├── README.md # Detailed explanation with: │ # • Intent & Problem │ # • Solution & Structure │ # • When to use/avoid │ # • Real-world examples │ # • Comparison with other patterns └── main_test.go # Optional: Tests showing usage
