Zero-magic, compile-time Dependency Injection for Go.
Dix scans your Go source code, builds a dependency graph from annotations in doc comments, and generates wiring code at dix/generated/root.go.
Instead of runtime reflection, Dix produces plain Go code that is easy to read, debug, and review.
- Compile-time safety: catch missing dependencies and circular dependency loops during generation.
- No reflection overhead: runtime performance stays close to handwritten wiring.
- Explicit output: generated code is transparent and maintainable.
- Simple workflow: a small CLI that fits naturally into
go runandgo buildflows.
go install github.com/smtdfc/dix@latestVerify installation:
dix --help- Scan source code for providers marked with
@Injectable. - Locate the composition root marked with
@Root. - Build a dependency graph.
- Generate
dix/generated/root.go. - Run
go run .orgo build <target>depending on the command you choose.
package app
type Repo struct{}
type Service struct {
repo *Repo
}
// @Injectable
func NewRepo() *Repo {
return &Repo{}
}
// @Injectable
func NewService(repo *Repo) *Service {
return &Service{repo: repo}
}
// @Injectable
// @Root
func NewMessage(service *Service) string {
_ = service
return "Dix wiring done"
}package main
import (
"fmt"
"github.com/your-org/your-app/dix/generated"
)
func main() {
msg := generated.Root()
fmt.Println(msg)
}dix run .- Parses source, generates wiring, then executes
go run .. directoryis optional and defaults to..
Examples:
dix run .
dix run ./internal/app- Parses source, generates wiring, then executes
go build <target>. targetis optional and defaults tomain.go.directoryis optional and defaults to..
Examples:
dix build
dix build main.go .
dix build cmd/api/main.go ./internalMarks a function as a valid provider in the dependency graph.
Marks the composition root where Dix starts traversing dependencies.
Important rules:
@Rootmust be used together with@Injectable.- A graph should have one root.
- A provider should return exactly one value.
- Dependency types must match exactly (
Tis different from*T).
dix/generated/root.go: generated wiring code.scan_<timestamp>.dix: scan metadata artifact.
- Getting started:
docs/docs/getting-started.md - Installation:
docs/docs/installation.md - Annotations:
docs/docs/annotations.md - Build and run:
docs/docs/build.md - Common errors:
docs/docs/common-errors.md
git clone https://github.com/smtdfc/dix.git
cd dix
go test ./...Open an issue or pull request if you want to improve features or docs.
MIT. See LICENSE.