« Back to Search  |  Reader Mode  |  Switch to Full Retro Site View Links: Stay in Reader  |  Retro Mode: ON  |  Original »
GitHub · Extracted in 759 ms

GitHub - floatdrop/di: Go 1.27+ dependency injection library

Source: https://github.com/floatdrop/di

Repository navigation

di
Public
forked from yandex/di

About

Go 1.27+ dependency injection library

yandex.github.io/di/

Resources

Readme License

Contributing

Contributing Activity

Stars

0 stars

Watchers

0 watching

Forks

0 forks Report repository
Branches

Tags
Go to Branches page Go to Tags page    

Latest commit

 

History


185 Commits
View commit history for this file.

Folders and files

Name Name Last commit message Last commit date
.github .github    
benchmarks benchmarks    
digrpc digrpc    
dihttp dihttp    
dislog dislog    
docs docs    
examples examples    
scripts scripts    
site site    
testdata/ fuzz testdata/ fuzz    
.gitignore .gitignore    
.golangci.yml .golangci.yml    
AUTHORS AUTHORS    
CHANGELOG.md CHANGELOG.md    
CLAUDE.md CLAUDE.md    
CONTRIBUTING.md CONTRIBUTING.md    
LICENSE LICENSE    
NOTICES NOTICES    
README.md README.md    
binding.go binding.go    
concurrent_test.go concurrent_test.go    
cycles_test.go cycles_test.go    
di.go di.go    
di_test.go di_test.go    
drain_test.go drain_test.go    
edges_internal_test.go edges_internal_test.go    
edges_test.go edges_test.go    
example_features_test.go example_features_test.go    
example_test.go example_test.go    
explain.go explain.go    
explain_test.go explain_test.go    
fixtures_test.go fixtures_test.go    
go.mod go.mod    
graph_internal_test.go graph_internal_test.go    
lifecycle.go lifecycle.go    
lifecycle_test.go lifecycle_test.go    
lifecyclemodel_test.go lifecyclemodel_test.go    
lifetime_test.go lifetime_test.go    
machine_test.go machine_test.go    
module_test.go module_test.go    
modules_test.go modules_test.go    
norace_internal_test.go norace_internal_test.go    
observe_test.go observe_test.go    
property_test.go property_test.go    
race_internal_test.go race_internal_test.go    
ready_test.go ready_test.go    
request_test.go request_test.go    
resolve.go resolve.go    
run.go run.go    
run_test.go run_test.go    
run_unix_test.go run_unix_test.go    
scheduler_test.go scheduler_test.go    
seal_internal_test.go seal_internal_test.go    
state.go state.go    
teardown_test.go teardown_test.go    
testhelper_test.go testhelper_test.go    
validate.go validate.go    
validate_test.go validate_test.go    
wire_bench_test.go wire_bench_test.go    
wire_internal_test.go wire_internal_test.go    
wire_test.go wire_test.go    
wiring_test.go wiring_test.go    
worker_test.go worker_test.go    
workerhook_test.go workerhook_test.go    
wrap_internal_test.go wrap_internal_test.go    
wrap_test.go wrap_test.go    

Repository files navigation

di

A dependency-injection container for Go 1.27+. Constructors are plain functions, keys are Go types, and services are built, started and stopped in dependency order. No code generation or dependencies outside the standard library.

Installation

go get golang.yandex/di

Requires Go 1.27 or newer. Editor support for generic methods needs gopls v0.23 or newer.

Quick start

A complete program using stand-in database and server types:

// Quick start: register a few services, start and stop the application.
package main

import (
	"context"
	"fmt"
	"log"

	"golang.yandex/di"
)

type Config struct{ DSN string }
type DB struct{ dsn string }
type Repo struct{ db *DB }
type Server struct{ repo *Repo }

// Plain constructors: their parameters are their dependencies.
func NewDB(cfg Config) *DB         { return &DB{dsn: cfg.DSN} }
func NewRepo(db *DB) *Repo         { return &Repo{db: db} }
func NewServer(repo *Repo) *Server { return &Server{repo: repo} }

func main() {
	app := di.New()

	app.Value(Config{DSN: "postgres://localhost/app"})

	app.Wire[*DB](NewDB).
		OnStop(func(ctx context.Context, db *DB) error { fmt.Println("db closed"); return nil })

	app.Wire[*Repo](NewRepo)

	app.Wire[*Server](NewServer).
		Eager().
		OnStart(func(ctx context.Context, srv *Server) error { fmt.Println("listening"); return nil }).
		OnStop(func(ctx context.Context, srv *Server) error { fmt.Println("server stopped"); return nil })

	ctx := context.Background()
	if err := app.Start(ctx); err != nil {
		log.Fatal(err)
	}
	defer func() {
		if err := app.Stop(ctx); err != nil {
			log.Println(err)
		}
	}()

	fmt.Println("serving", app.Get[*Server]().repo.db.dsn)
}

Value registers an existing value; Wire registers a constructor whose parameters are its dependencies. Registrations are lazy singletons by default. Eager() builds the server during Start, resolving its dependencies first. Stop runs the stop hooks in reverse build order: server before database.

The source is examples/quickstart/main.go . For a long-running application, Run starts, waits for a signal or shutdown request, then stops with a deadline; see the HTTP server .

Capabilities

API
Registration Plain constructors with Wire, scope-aware closures with Provide, existing values with Value.
Scopes Shared singletons or one instance per resolving scope with Scoped().
Lifecycle Start, drain and stop hooks; background workers; signal handling with Run.
Optional dependencies and groups Declare optional parameters and group slices with Needs.
Modules and wrappers Compose registration functions with Use; decorate services with Wrap.
Testing Fresh scopes with di.Test and explicit Override() registrations.
Validation and inspection Check declared dependencies before construction; render dependency trees, Graphviz DOT and module reports.
HTTP and gRPC Request scopes, handler resolution and server lifecycle with dihttp and digrpc .
Observability Constructor and hook events with Observe; log/slog integration with dislog .

Wire reads constructor signatures once with reflection, so Validate can check their dependencies without building anything. Provide closures are checked only when they run and are reported as unchecked by Validate.

Documentation

Performance

The benchmarks/ module compares the same four-service graph on an Apple M3 Max:

Warm resolve Cold register and build
di, Provide closure 21 ns, 0 B, 0 allocs 4.0 µs, 4.4 kB, 68 allocs
di, Wire 21 ns, 0 B, 0 allocs 5.1 µs, 5.4 kB, 86 allocs
do v2.1 134 ns, 160 B, 6 allocs 7.0 µs, 11.2 kB, 120 allocs
dig v1.19 486 ns, 768 B, 24 allocs 19.9 µs, 24.3 kB, 302 allocs

The dig warm figure measures reflective Invoke, not a typed accessor. An fx application normally invokes once at startup; the cold comparison is the fairer one.

cd benchmarks && go test -bench . -benchmem

Versioning

While the major version is 0, a minor bump may change behaviour. CHANGELOG.md and the releases state whether an upgrade can break a caller.

Contributing

See CONTRIBUTING.md for the CLA, tests and example-sync instructions.

License

MIT

About

Go 1.27+ dependency injection library

yandex.github.io/di/

Resources

Readme License

Contributing

Contributing Activity

Stars

0 stars

Watchers

0 watching

Forks

0 forks Report repository

Releases

Packages

Contributors

Languages