samber-do

Wire Go dependency injection containers with samber/do provider functions and scopes.

1|2|Updated Nov 25, 2017
One-click install
npx skills add https://github.com/asarchami/dotfiles --skill samber-do-asarchami
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: samber-do
Source: https://github.com/asarchami/dotfiles/tree/main/dot_config/opencode/skills/samber/samber-do
Command: npx skills add https://github.com/asarchami/dotfiles --skill samber-do-asarchami

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires github.com/samber/do/v2, and includes references (resource) components.

What problem does it solve? Manually wiring Go services at the composition root becomes fragile as applications grow, and ad-hoc dependency injection leads to concrete-type coupling, hidden failures, and leaked request-scoped state. This Skill guides you through registering, resolving, and testing services with the samber/do/v2 DI container. ## Core Features & Use Cases - Provider-based registration: Register services with lazy, eager, value, or transient lifecycles using provider functions, and resolve them by interface via implicit aliasing. - Module and scope organization: Group registrations into packages with do.Package, compose them at the root, and isolate request-scoped services in child scopes. - Lifecycle and testing support: Wire graceful shutdown with signal handling, run health checks, and clone containers with overrides for isolated tests. - Use Case: You are refactoring a Go service that constructs its dependency graph by hand in main. Use this Skill to convert each constructor into a provider, group them into infrastructure and service packages, and resolve the API server by interface with graceful shutdown. ## Quick Start Ask the AI to refactor your Go main function to register all services with samber/do providers and resolve the server by interface.

Frequently Asked Questions about samber-do

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

FAQPage Schema
How do I register services with samber/do in Go?

Register services with do.Provide using a provider function of signature func(i do.Injector) (T, error). Use do.ProvideValue for pre-created config, do.ProvideTransient for per-request instances, and do.Eager for startup-critical services.

How do I resolve a service by interface in samber/do?

Register the concrete type with do.Provide, then resolve it with do.MustInvokeAs[Interface](injector). This uses implicit aliasing so one binding serves both the concrete type and the interface.

Should I use samber/do v1 or v2?

Use v2 by importing github.com/samber/do/v2. Importing the v1 path is a common mistake; v2 provides the generic API with do.Provide, do.Invoke, scopes, and packages described in this Skill.

How do I test services that use samber/do?

Clone the container per test with do.Clone or build a fresh do.New(), then replace dependencies using do.OverrideValue or do.Override. Each test runs against an isolated container and overrides only what it exercises.

When should I avoid do.MustInvoke?

Avoid MustInvoke whenever a service may be absent or its provider can fail, since it panics on error. Use do.Invoke, which returns (T, error), and handle the error; reserve MustInvoke for resolutions where failure is impossible.

How do I shut down samber/do services gracefully?

Implement the Shutdowner interface on your services, then call injector.ShutdownOnSignalsWithContext(ctx, os.Interrupt) or ShutdownWithContext with a timeout. Request-scoped services are released when their scope ends.