dix

dix is a lightweight yet powerful dependency injection framework for Go.
Inspired by uber-go/dig, with support for advanced dependency management and namespace isolation.
δΈζζζ‘£
β¨ Features
| Feature |
Description |
| π Cycle Detection |
Auto-detect dependency cycles |
| π¦ Multiple Injection |
Support func, struct, map, list |
| π·οΈ Namespace |
Dependency isolation via map key |
| π― Multi-Output |
Struct can provide multiple dependencies |
| πͺ Nested Support |
Support nested struct injection |
| π§ Non-Invasive |
Zero intrusion to original objects |
| π‘οΈ Safe API |
TryProvide/TryInject won't panic |
| π Visualization |
HTTP module for dependency graph |
π¦ Installation
go get github.com/pubgo/dix/v2
π Quick Start
package main
import (
"fmt"
"github.com/pubgo/dix/v2"
)
type Config struct {
DSN string
}
type Database struct {
Config *Config
}
type UserService struct {
DB *Database
}
func main() {
// Create container
di := dix.New()
// Register Providers
dix.Provide(di, func() *Config {
return &Config{DSN: "postgres://localhost/mydb"}
})
dix.Provide(di, func(c *Config) *Database {
return &Database{Config: c}
})
dix.Provide(di, func(db *Database) *UserService {
return &UserService{DB: db}
})
// Inject and use
dix.Inject(di, func(svc *UserService) {
fmt.Println("DSN:", svc.DB.Config.DSN)
})
}
π Core API
Provide / TryProvide
Register constructor (Provider) to container:
// Standard version - panics on error
dix.Provide(di, func() *Service { return &Service{} })
// Safe version - returns error
err := dix.TryProvide(di, func() *Service { return &Service{} })
if err != nil {
log.Printf("Registration failed: %v", err)
}
Inject / TryInject
Inject dependencies from container:
// Function injection
dix.Inject(di, func(svc *Service) {
svc.DoSomething()
})
// Struct injection
type App struct {
Service *Service
Config *Config
}
app := &App{}
dix.Inject(di, app)
// Safe version
err := dix.TryInject(di, func(svc *Service) {
// ...
})
π― Injection Patterns
Struct Injection
type In struct {
Config *Config
Database *Database
}
type Out struct {
UserSvc *UserService
OrderSvc *OrderService
}
// Multiple inputs and outputs
dix.Provide(di, func(in In) Out {
return Out{
UserSvc: &UserService{DB: in.Database},
OrderSvc: &OrderService{DB: in.Database},
}
})
Map Injection (Namespace)
// Provide with namespace
dix.Provide(di, func() map[string]*Database {
return map[string]*Database{
"master": &Database{DSN: "master-dsn"},
"slave": &Database{DSN: "slave-dsn"},
}
})
// Inject specific namespace
dix.Inject(di, func(dbs map[string]*Database) {
master := dbs["master"]
slave := dbs["slave"]
})
List Injection
// Provide same type multiple times
dix.Provide(di, func() []Handler {
return []Handler{&AuthHandler{}}
})
dix.Provide(di, func() []Handler {
return []Handler{&LogHandler{}}
})
// Inject all
dix.Inject(di, func(handlers []Handler) {
// handlers contains AuthHandler and LogHandler
})
π§© Modules
dixglobal - Global Container
Provides global singleton container for simple applications:
import "github.com/pubgo/dix/v2/dixglobal"
// Use directly without creating container
dixglobal.Provide(func() *Config { return &Config{} })
dixglobal.Inject(func(c *Config) { /* ... */ })
dixcontext - Context Integration
Bind container to context.Context:
import "github.com/pubgo/dix/v2/dixcontext"
// Store in context
ctx := dixcontext.Create(context.Background(), di)
// Retrieve and use
container := dixcontext.Get(ctx)
dixhttp - Dependency Visualization π
Web interface for visualizing dependency graph, designed for large projects:
import (
"github.com/pubgo/dix/v2/dixhttp"
"github.com/pubgo/dix/v2/dixinternal"
)
server := dixhttp.NewServer((*dixinternal.Dix)(di))
server.ListenAndServe(":8080")
Visit http://localhost:8080 to view dependency graph.
Highlights:
- π Fuzzy Search - Quickly locate types or functions
- π¦ Package Grouping - Collapsible sidebar browsing
- π Bidirectional Tracking - Show both dependencies and dependents
- π Depth Control - Limit display levels (1-5 or all)
- π¨ Modern UI - Tailwind CSS + Alpine.js
See dixhttp/README.md for details.
π οΈ Development
# Run tests
task test
# Lint
task lint
# Build
task build
π Examples
π Documentation
π License
MIT