golang-dependency-injection

Designs and implements dependency injection patterns in Go using manual constructors or DI libraries.

1|Updated May 25, 2020
One-click install
npx skills add https://github.com/titaneric/dotfiles --skill golang-dependency-injection-titaneric
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-dependency-injection
Source: https://github.com/titaneric/dotfiles/tree/main/dot_agents/skills/golang-dependency-injection
Command: npx skills add https://github.com/titaneric/dotfiles --skill golang-dependency-injection-titaneric

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go applications often suffer from tightly coupled services, global variables, and untestable code. This Skill guides you to structure Go services with proper dependency injection, making them testable, loosely coupled, and maintainable. ## Core Features & Use Cases - DI Architecture Guidance: Enforces constructor injection, interfaces defined at consumption sites, and composition-root-only containers while flagging anti-patterns like service locators and global state. - Library Decision Support: Compares manual injection, google/wire, uber-go/dig + fx, and samber/do across type safety, lifecycle management, lazy loading, and learning curve with a decision table. - Testing Patterns: Shows how to inject mocks at interface boundaries and use container cloning with overrides for integration tests. - Use Case: When refactoring a Go microservice with 40+ coupled services, the Skill orchestrates sub-agents to find globals, map concrete dependencies, detect service locators, and produce a migration plan. ## Quick Start Ask the agent to review your Go service architecture and recommend a dependency injection approach with wiring code for your project.

Frequently Asked Questions about golang-dependency-injection

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

FAQPage Schema
How do I implement dependency injection in Go?

Pass dependencies through constructors like NewUserService(db, mailer, logger) and wire them explicitly in main(). For small projects under 10 services, manual constructor injection is sufficient; larger projects benefit from a DI library.

google/wire vs uber-go/fx vs samber/do, which DI library should I use?

google/wire offers compile-time safety via code generation but no lifecycle management. uber-go/fx provides full lifecycle hooks but uses reflection. samber/do gives compile-time generics safety with built-in health checks and shutdown, requiring Go 1.18+.

When should I use a DI library instead of manual injection in Go?

Stay with manual constructor injection for projects under 10 services. Consider a library at 10-20 services with cross-cutting concerns, and strongly adopt one beyond 20 services or when you need health checks, graceful shutdown, or lazy loading.

Why is passing the DI container as a dependency a bad pattern?

Passing the container into services is the service locator anti-pattern, hiding real dependencies and making testing harder. The container should exist only at the composition root in main(), with services receiving specific dependencies via constructors.

How do I test Go services that use dependency injection?

Define a mock implementing the consumer-side interface and inject it through the constructor in tests. With samber/do, clone the container and use do.OverrideValue to replace only the boundary services like databases or mailers.

Does google/wire support lifecycle management and lazy loading?

No. Wire generates plain Go constructor calls with all dependencies created eagerly and no built-in health checks or shutdown hooks. Cleanup relies on functions returned from providers, while fx and samber/do offer built-in lifecycle support.