Skip to main content

✌️ Dependency Injection in Go

Dependency injection (DI) in Go is the practice of supplying a component with the services it depends on, instead of letting it construct them itself — making code easier to test, swap, and reason about. samber/do is a DI toolkit for Go that implements this pattern using 1.18+ generics instead of reflection, giving you a type-safe API with no code generation and zero external dependencies.

samber/do — type-safe dependency injection for Go using generics

See also:

  • samber/lo: A Lodash-style Go library based on Go 1.18+ Generics
  • samber/mo: Monads based on Go 1.18+ Generics (Option, Result, Either...)

Why this name?

I love the short name for such a utility library. This name is the sum of DI and Go and no Go package uses this name.

samber/do vs uber/dig vs uber/fx vs google/wire

samber/douber/diguber/fxgoogle/wireManual DI
MechanismGenericsReflectionReflection (built on dig)Code generationHand-written
Type safetyCompile-timeRuntimeRuntimeCompile-timeCompile-time
Code generationNoneNoneNoneRequired (wire CLI)None
Lifecycle managementHealth checks, graceful shutdown, hooksNone built-inStart/stop hooksNoneHand-written
Scopes / modulesFull scope treeLimitedModulesProvider setsHand-written
Debugging toolsScope tree, dependency graph, Web UINone built-infx.WithLoggerNone (inspect generated code)N/A
External dependenciesNoneNoneSeveral (dig, zap, ...)wire build toolNone

When to use samber/do: you want compile-time-checked service resolution without a build step, plus built-in health checks and graceful shutdown for production services. See the detailed comparisons: do vs uber/fx, do vs google/wire, and do vs uber/dig. Already using one of these? Check the migration guides.

💡 Features

samber/do is built with high attention to the developer and contributor experience.

Where to go next