Labsco
samber logo

golang-samber-do

2,400

by samber · part of samber/cc-skills-golang

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.

🔥🔥🔥✓ VerifiedFreeQuick setup
🧩 One of 7 skills in the samber/cc-skills-golang package — works on its own, and pairs well with its siblings.

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.

Inspect the full instructions your agent will receiveExpand

This is the exact playbook injected into your agent when the skill activates — shown here so you can audit it before installing. You don't need to read it to use the skill.

by samber

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 https://github.com/samber/cc-skills-golang --skill golang-samber-do Download ZIPGitHub2.4k 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.

Using samber/do for Dependency Injection in Go

Type-safe dependency injection toolkit for Go based on Go 1.18+ generics.

Official Resources:

This skill is not exhaustive. Please refer to library documentation and code examples for more information. Context7 can help as a discoverability platform. For Go package docs, versions, symbols, and known vulnerabilities, → See samber/cc-skills-golang@golang-pkg-go-dev skill.

DO NOT USE v1 OF THIS LIBRARY. INSTALL v2 INSTEAD:

Copy & paste — that's it
go get -u github.com/samber/do/v2

Core Concepts

The Injector (Container)

Copy & paste — that's it
import "github.com/samber/do/v2"

injector := do.New()

Service Types

  • Lazy (default): Created when first requested

  • Eager: Created immediately when the container starts

  • Transient: New instance created on every request

  • Value: Pre-created value, no instantiation

Provider Functions

Services MUST be registered via provider functions:

Copy & paste — that's it
type Provider[T any] func(i Injector) (T, error)

Package Organization

Use do.Package() to organize service registration by module:

Copy & paste — that's it
// infrastructure/package.go
var Package = do.Package(
 do.Lazy(func(i do.Injector) (*postgres.DB, error) {
 cfg := do.MustInvoke[*Config](i)
 return postgres.Connect(cfg.DatabaseURL)
 }),
 do.Lazy(func(i do.Injector) (*redis.Client, error) {
 cfg := do.MustInvoke[*Config](i)
 return redis.NewClient(cfg.RedisURL), nil
 }),
)

// main.go
injector := do.New(infrastructure.Package, service.Package)

Best Practices

  • Depend on interfaces, not concrete types — lets you swap implementations in tests without touching production code

  • Each service should have one job — services with multiple responsibilities are harder to test and harder to replace

  • Keep dependency trees shallow — chains beyond 3-4 levels make initialization order fragile and errors harder to trace

  • Handle errors in provider functions — a silently failing provider creates a broken service that crashes later in unexpected places

  • Use scopes to organize services by lifecycle — request-scoped services prevent leaks, global services prevent redundant initialization

For scopes, lifecycle management, struct injection, and debugging, see Advanced Usage.

For testing patterns (cloning, overrides, mocks), see Testing.

Quick Reference

Registration

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

Invocation

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)

Cross-References

  • → See samber/cc-skills-golang@golang-dependency-injection skill for DI concepts, comparison, and when to adopt a DI library

  • → See samber/cc-skills-golang@golang-structs-interfaces skill for interface design patterns

  • → See samber/cc-skills-golang@golang-testing skill for general testing patterns