golang-samber-do

Implements dependency injection in Go using the samber/do v2 container library.

Updated Jun 30, 2026
One-click install
npx skills add https://github.com/santoshkal/chezmoi --skill golang-samber-do-santoshkal
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: golang-samber-do
Source: https://github.com/santoshkal/chezmoi/tree/main/private_dot_config/opencode/skills/Golang/skills/golang-samber-do
Command: npx skills add https://github.com/santoshkal/chezmoi --skill golang-samber-do-santoshkal

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Setting up dependency injection in Go often leads to tangled manual constructor wiring, wrong library versions (v1 vs v2), and anti-patterns like passing the container into business logic. This Skill guides correct adoption of samber/do v2 with type-safe generics-based registration, lifecycle management, and testing patterns. ## Core Features & Use Cases - Service Registration & Resolution: Register lazy, eager, transient, value, and named services, then invoke them by type or interface using implicit aliasing with InvokeAs. - Lifecycle & Scopes: Organize services with do.Package modules and child scopes, implement Healthchecker and Shutdowner interfaces, and handle graceful shutdown on OS signals. - Testing Support: Clone containers and override services with mocks for isolated unit tests. - Use Case: When refactoring a Go web app from manual constructor injection in main.go, use this Skill to restructure registrations into per-module packages with request-scoped services and signal-based graceful shutdown. ## Quick Start Ask the AI to set up dependency injection in your Go project using samber/do v2 with a container, a database service, and graceful shutdown.

Frequently Asked Questions about golang-samber-do

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up dependency injection in Go with samber/do?▼

Install the v2 library with go get github.com/samber/do/v2, create a container with do.New(), and register services via provider functions using do.Provide. Resolve dependencies at the composition root with do.Invoke or do.MustInvoke.

Should I use samber/do v1 or v2?▼

Always use v2 (github.com/samber/do/v2). The v1 API is deprecated, and v2 is built on Go 1.18+ generics for type-safe registration and invocation of services.

How do I register multiple services of the same type in samber/do?▼

Use do.ProvideNamed to register each instance under a distinct name, such as primary-db and replica-db, then retrieve them with do.InvokeNamed or do.MustInvokeNamed. This avoids overwriting registrations of the same type.

How do I mock dependencies for testing with samber/do?▼

Clone the production container with injector.Clone(), then replace real services with mocks using do.Override or do.OverrideValue. Invoke the service under test from the cloned container so tests stay isolated from the original.

Is passing the do.Injector into handlers a good practice?▼

No, passing the injector into business logic creates a service locator anti-pattern that hides dependencies. Access the container only at the composition root and pass resolved dependencies to handlers as constructor parameters.

How do I gracefully shut down services registered in samber/do?▼

Implement a Shutdown method on services needing cleanup, with optional context and error returns. Then call injector.ShutdownOnSignalsWithContext with os.Interrupt to shut down all services when the application receives a signal.