Learn
Getting Started
Installation
Your First App
Step 1: Define Your Services
Step 2: Wire Everything with godi
Step 3: Run It
Why This Matters
Without godi
With godi
Using Modules (Recommended)
For Web Applications
Testing is Easy
Next Steps
Quick Tips
Core Concepts
1. Services
2. Constructors
3. Lifetimes
Singleton - One Forever
Scoped - One Per Request
Transient - Always New
Quick Reference
4. Modules
5. Scopes
Putting It All Together
Common Patterns
Pattern 1: Shared Infrastructure, Request-Specific Logic
Pattern 2: Test Modules
Pattern 3: Environment-Specific Modules
Quick Decision Guide
Summary
Guides
Web Applications with net/http
The Controller Pattern
Setting Up Middleware
Complete Example: Blog API
Step 1: Models and Services
Step 2: Request Context Service
Step 3: Controllers
Step 4: Wire Everything Together
Step 5: Test the API
Testing Controllers
Best Practices
1. Use Middleware for Cross-Cutting Concerns
2. Controllers for Complex Handlers
3. Keep Controllers Focused
Summary
Web Applications with Gin
The Controller Pattern with Gin
Setting Up Middleware
Complete Example: Blog API
Step 1: Models and Services
Step 2: Request Context Service
Step 3: Controllers
Step 4: Wire Everything Together
Step 5: Test the API
Testing with Gin
Advanced Patterns
Authentication Middleware
Error Handler Middleware
Using Request Context in Services
Best Practices
1. Use Gin’s Built-in Features
2. Group Related Routes
3. Leverage Both Contexts
Summary
Web Applications with Gorilla Mux
The Controller Pattern with Mux
Setting Up Middleware
Complete Example: Blog API
Step 1: Models and Services
Step 2: Request Context Service
Step 3: Controllers
Step 4: Wire Everything Together
Step 5: Test the API
Advanced Patterns
Authentication Middleware
CORS Middleware
Route Groups with Different Middleware
Testing with Mux
Best Practices
1. Use Mux Features
2. Structured Route Organization
3. Error Handling
Summary
Testing Guide
The Basic Pattern
Creating Mocks
Simple Mock
Mock with Behavior Control
Spy Pattern for Verification
Test Helpers
Table-Driven Tests
Testing Error Scenarios
Testing with Scopes
Integration Testing
Testing HTTP Handlers
Benchmark Testing
Best Practices
1. Use Interfaces for Mocking
2. Create Test Modules for Common Scenarios
3. Test One Thing at a Time
4. Use t.Cleanup for Resources
Common Testing Patterns
Assert Mock Calls
Test with Context
Summary
Modules Guide
Why Use Modules?
Basic Module
Module Dependencies
Advanced Features
Keyed Services
With Parameter Objects
Service Groups
Real Example: Middleware Chain
Parameter Objects (In/Out)
Input Parameters (godi.In)
Output Parameters (godi.Out)
Resource Disposal
Disposal Order
Scoped Disposal Example
Register As Interface
Mixed Lifetime Groups
Advanced Patterns
Factory Pattern
Lazy Loading
Context Enrichment
Performance Considerations
Singleton vs Scoped
Minimizing Allocations
When to Use Advanced Features
Summary
Reference
API Reference
Collection
Creating a Collection
Registering Services
Building Provider
Query Methods
Provider
Resolution Methods (Generic)
Resolution Methods (Reflection)
Scope Management
Scope
Modules
Creating Modules
Module Builders
Registration Options
Name Option
Group Option
As Option
Combining Options
Parameter Objects
Input Parameters (In)
Output Parameters (Out)
Disposal Interface
Provider Options
Lifetimes
Error Types
Sentinel Errors
Complete Example
Error Reference
Error Types
Common Errors
ResolutionError
CircularDependencyError
LifetimeConflictError
Disposed Errors
Constructor Errors
Error Handling Patterns
Basic Error Checking
Type-Specific Handling
Graceful Degradation
Build-Time Validation
Sentinel Errors
Debugging Tips
1. Check Registration
2. Check Lifetime Dependencies
3. Check Circular Dependencies
4. Enable Detailed Logging
Common Scenarios
Service Not Found
Circular Dependency
Scope Disposed
Lifetime Conflict
Summary
Frequently Asked Questions
General Questions
What is godi?
When should I use dependency injection?
How is godi different from other DI libraries?
Getting Started
How do I install godi?
What’s the simplest example?
Should I always use modules?
Concepts
What’s the difference between Singleton, Scoped, and Transient?
When should I use scopes?
What are keyed services?
Common Issues
“Service not found” error
“Circular dependency” error
“Scope disposed” error
Can Singleton depend on Scoped?
Testing
How do I mock services for testing?
How do I test with different scenarios?
Performance
Is DI slow?
How can I optimize performance?
Does godi use reflection?
Best Practices
How should I structure modules?
Should I use parameter objects?
How do I handle optional dependencies?
Advanced
Can I register multiple interfaces for one type?
How do I create services dynamically?
Can services be disposed automatically?
Troubleshooting
How do I debug dependency resolution?
How do I visualize dependencies?
Need More Help?
Changelog
3.0.0 (2025-07-28)
⚠ BREAKING CHANGES
Features
2.1.0 (2025-07-22)
Features
2.0.5 (2025-07-22)
Bug Fixes
2.0.4 (2025-07-22)
Bug Fixes
2.0.3 (2025-07-22)
Bug Fixes
2.0.2 (2025-07-21)
Bug Fixes
2.0.1 (2025-07-21)
Bug Fixes
2.0.0 (2025-07-21)
⚠ BREAKING CHANGES
Features
1.6.2 (2025-07-17)
Bug Fixes
1.6.1 (2025-07-17)
Bug Fixes
1.6.0 (2025-07-17)
Features
1.5.1 (2025-07-17)
Bug Fixes
1.5.0 (2025-07-17)
Features
1.4.0 (2025-07-17)
Features
1.3.3 (2025-07-16)
Bug Fixes
1.3.2 (2025-07-16)
Bug Fixes
1.3.1 (2025-07-16)
Bug Fixes
1.3.0 (2025-07-16)
Features
1.2.2 (2025-07-16)
1.2.1 (2025-07-15)
1.2.0 (2025-07-14)
1.1.0 (2025-07-14)
⚠ BREAKING CHANGES
Features
1.0.2 (2025-07-13)
1.0.1 (2025-07-12)
1.0.0 (2025-07-11)
Quick Links
GitHub
pkg.go.dev
Go Report Card
Community
Discussions
Issues
godi
Index
Edit on GitHub
Index