npx skills add ...
npx skills add samber/cc-skills-golang --skill golang-samber-do
Dependency injection in Golang using samber/do — service containers, lifecycle management, scopes, health checks, graceful shutdown, and module organization. Apply when using or adopting samber/do, when the codebase imports github.com/samber/do or github.com/samber/do/v2, or when refactoring manual constructor injection into a DI container.
npx skills add samber/cc-skills-golang --skill golang-samber-do
Persona: You are a Go architect setting up dependency injection. You keep the container at the composition root, depend on interfaces not concrete types, and treat provider errors as first-class failures.
Type-safe dependency injection toolkit for Go based on Go 1.18+ generics.
Official Resources:
This skill is not exhaustive — refer to library documentation and code examples for more information:
samber/cc-skills-golang@golang-pkg-go-dev skill (godig), preferred over Context7 for Go package facts.samber/cc-skills-golang@golang-gopls skill (gopls).Install v2 — v1 is superseded and lacks the generics-based container, scopes, and lifecycle hooks documented below, so v1-era guidance misleads on every API in this skill:
Services MUST be registered via provider functions:
Follow "Accept Interfaces, Return Structs":
The container MUST only be accessed at the composition root:
Inside a provider function, always use do.MustInvoke (or MustInvokeAs/MustInvokeNamed/MustInvokeStruct) rather than the error-returning variant:
(T, error), so propagating a dependency failure with do.Invoke costs an extra if err != nil { return nil, err } on every call.do.MustInvoke panics instead, but samber/do correctly catches and recovers that panic at the enclosing Invoke call and converts it back into a regular error — this recover happens inside the library itself, not in caller code, so MustInvoke is safe to use inside providers.Register a concrete type and invoke as an interface without explicit aliasing:
Register multiple services of the same type:
Use do.Package() to organize service registration by module:
do.MustInvoke* inside provider functions instead of do.Invoke* — samber/do correctly catches and recovers the panic at the outer Invoke call, turning it back into a returned error, so it's safe to use inside providers and you get the same error propagation without the boilerplateFor scopes, lifecycle management, struct injection, and debugging, see Advanced Usage.
For testing patterns (cloning, overrides, mocks), see Testing.
| Function | Purpose |
|---|---|
do.Provide[T]() | Register lazy service (default) |
do.ProvideNamed[T]() | Register named lazy service |
do.ProvideValue[T]() | Register pre-created value |
do.ProvideNamedValue[T]() | Register named value |
do.ProvideTransient[T]() | Register new instance each time |
do.ProvideNamedTransient[T]() | Register named transient service |
do.Package() | Group service registrations |
| Function | Purpose |
|---|---|
do.Invoke[T]() | Get service (with error) |
do.InvokeNamed[T]() | Get named service |
do.InvokeAs[T]() | Get first service matching interface |
do.InvokeStruct[T]() | Inject into struct fields using tags |
do.MustInvoke[T]() | Get service (panic on error) |
do.MustInvokeNamed[T]() | Get named service (panic on error) |
do.MustInvokeAs[T]() | Get service by interface (panic on error) |
do.MustInvokeStruct[T]() | Inject into struct (panic on error) |
samber/cc-skills-golang@golang-dependency-injection skill for DI concepts, comparison, and when to adopt a DI librarysamber/cc-skills-golang@golang-structs-interfaces skill for interface design patternssamber/cc-skills-golang@golang-testing skill for general testing patterns