golang-samber-do

Set up dependency injection in Go using samber/do with provider functions and scopes.

2|Updated Feb 12, 2024
One-click install
npx skills add https://github.com/adibfirman/dotfiles --skill golang-samber-do-adibfirman
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-samber-do
Source: https://github.com/adibfirman/dotfiles/tree/main/claude/.claude/skills/technical/golang/golang-samber-do
Command: npx skills add https://github.com/adibfirman/dotfiles --skill golang-samber-do-adibfirman

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps you implement reliable dependency injection in Go using samber/do, avoiding tangled constructor wiring and reducing startup/runtime failures from hidden dependency issues.

Core Features & Use Cases

  • Service container with lifecycles: Register services as lazy (default), eager, transient, or pre-created values for the right creation timing.
  • Provider-based registrations: Use provider functions that return (service, error) so dependency failures are handled immediately and explicitly.
  • Composition-root organization: Keep container access confined to startup/composition root, while business code depends on interfaces.
  • Scopes and lifecycle management: Use scopes for module separation and implement health checks and graceful shutdown for managed services.
  • Operational patterns: Prefer implicit aliasing via InvokeAs, support named services, and use struct tag injection to reduce repetitive MustInvoke calls.

Quick Start

Register your dependencies at the composition root with do.New and do.Provide/do.ProvideValue, then resolve them with do.MustInvoke or InvokeAs where appropriate.

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 to avoid manual constructor wiring?

Dependency injection in Go using samber/do eliminates manual constructor wiring by registering provider functions returning (T, error) at the composition root, then resolving services via do.Invoke or MustInvoke. This confines container access to startup while business code depends on interfaces.

What is the best way to manage service lifecycles and scopes in a Go application?

Managing service lifecycles in Go involves registering services as lazy, eager, transient, or pre-created values using a service container. Scopes provide module separation, while health checks and graceful shutdown manage service availability and termination.

How does samber/do handle dependency resolution errors during application startup?

Dependency resolution errors are handled explicitly through provider functions that return (service, error). When using do.Invoke or MustInvoke, dependency failures surface immediately and are handled at the composition root, reducing hidden runtime issues.

Can I use interface resolution for dependency injection instead of concrete types in Go?

Interface resolution for dependency injection is supported by preferring InvokeAs for interfaces, which enables implicit aliasing. This allows business code to depend on abstractions rather than concrete struct implementations, keeping modules decoupled.

When do I need a service container with health checks and graceful shutdown in Go?

A service container with health checks and graceful shutdown is needed when structuring modular Go applications with package grouping and scopes. It ensures managed services terminate cleanly and report operational status during application lifecycle events.

How do I reduce repetitive MustInvoke calls when resolving dependencies in Go?

To reduce repetitive MustInvoke calls when resolving dependencies in Go, use struct tag injection. This feature minimizes boilerplate by automatically populating struct fields with registered services, streamlining the dependency resolution process at the composition root.