godi
Dependency injection that gets out of your way.
godi automatically wires up your Go applications. Define your services, specify their lifetimes, and let godi handle the rest.
services := godi.NewCollection()
services.AddSingleton(NewLogger)
services.AddSingleton(NewDatabase)
services.AddScoped(NewUserService)
provider, _ := services.Build()
defer provider.Close()
userService := godi.MustResolve[UserService](provider)
Why godi?
Feature |
Benefit |
|---|---|
Automatic wiring |
No manual constructor calls |
Three lifetimes |
Singleton, Scoped, Transient |
Compile-time safety |
Generic type resolution |
Zero codegen |
Pure runtime, no build steps |
Get Started in 5 Minutes
Install godi:
go get github.com/junioryono/godi/v4
Create your first container:
package main
import (
"fmt"
"github.com/junioryono/godi/v4"
)
type Logger struct{}
func (l *Logger) Log(msg string) { fmt.Println(msg) }
type UserService struct {
logger *Logger
}
func NewUserService(logger *Logger) *UserService {
return &UserService{logger: logger}
}
func main() {
services := godi.NewCollection()
services.AddSingleton(func() *Logger { return &Logger{} })
services.AddSingleton(NewUserService)
provider, _ := services.Build()
defer provider.Close()
users := godi.MustResolve[*UserService](provider)
users.logger.Log("Hello, godi!")
}
Ready for more? Start the Get Started in 5 Minutes.
Quick Links
Learning godi
Get Started in 5 Minutes - Build your first app in 5 minutes
Service Lifetimes - Singleton, Scoped, and Transient explained
Building Web Applications - Complete web app patterns
Framework Integrations
Gin Integration - Gin web framework
Chi Integration - Chi router
Echo Integration - Echo framework
Fiber Integration - Fiber framework
net/http Integration - Standard library
Advanced Features
Keyed Services - Multiple implementations
Parameter Objects - Simplify constructors
Modules - Organize large apps
License
MIT License - see LICENSE