godi - Dependency Injection with Service Lifetimes for Go
A sophisticated dependency injection container for Go with service lifetimes, type safety, and automatic dependency resolution.
Quick Example
// Define your services
func NewLogger() Logger { return &logger{} }
func NewDatabase(logger Logger) Database { return &database{logger} }
func NewUserService(db Database) UserService { return &userService{db} }
// Wire everything together
services := godi.NewCollection()
services.AddSingleton(NewLogger)
services.AddSingleton(NewDatabase)
services.AddScoped(NewUserService)
// Build and use
provider, _ := services.Build()
defer provider.Close()
userService := godi.MustResolve[UserService](provider)
Key Features
- Service Lifetimes
Singleton: One instance for the entire application
Scoped: One instance per scope (perfect for HTTP requests)
Transient: New instance every time
- Type Safety
Generic resolution with compile-time type checking
No runtime type assertions needed
Full IDE autocomplete support
- Automatic Resolution
Analyzes constructors and builds dependency graph
Detects circular dependencies at build time
Validates lifetime rules before runtime
- Advanced Features
Keyed Services: Multiple implementations of the same interface
Service Groups: Batch operations on related services
Parameter Objects: Clean constructors with
godi.InResult Objects: Register multiple services with
godi.OutModules: Organize services into reusable packages
Getting Started
Installation
go get github.com/junioryono/godi/v4
Requirements: Go 1.21 or later
Start with our Installation guide to set up godi in your project.
Why godi?
Zero Code Generation: Pure runtime dependency injection
Thread-Safe: Fully concurrent-safe operations
Production Ready: Battle-tested in real applications
Clean API: Intuitive and idiomatic Go
Excellent Errors: Detailed error messages for debugging
Next Steps
Installation - Set up godi in your project
Core Concepts - Understand the fundamentals
License
MIT License - see LICENSE