golang-dependency-injection

Guide Go dependency injection design to eliminate hidden coupling.

1|Updated May 27, 2026
One-click install
npx skills add https://github.com/dmwin72015/netdisk --skill golang-dependency-injection-dmwin72015
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-dependency-injection
Source: https://github.com/dmwin72015/netdisk/tree/main/.agents/skills/golang-dependency-injection
Command: npx skills add https://github.com/dmwin72015/netdisk --skill golang-dependency-injection-dmwin72015

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

It prevents tightly coupled Go code by ensuring dependencies are passed explicitly, which improves testability, maintainability, and predictable lifecycle management.

Core Features & Use Cases

  • Constructor-based DI: Prefer manual constructor injection for small projects and inject dependencies instead of using globals or init().
  • Interface-first design: Define interfaces at the consumption site to decouple services from concrete implementations.
  • Correct DI container usage: Keep the container/composition root at app startup only, avoid passing the injector into services, and use lazy creation with singletons for stateful services.
  • DI library decision support: Select among google/wire, uber-go/dig + fx, and samber/do based on project size, lifecycle needs, and type-safety constraints.
  • Testing patterns: Mock at the interface boundary and, when using samber/do, clone and override providers for isolated tests.

Quick Start

Ask the AI to refactor your Go service wiring to use constructor injection (no globals or init()), define interfaces at the consumer boundary, and generate the recommended DI wiring code for either manual injection or a DI library (based on your number of services and lifecycle needs).

Frequently Asked Questions about golang-dependency-injection

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

FAQPage Schema
How do I eliminate hidden coupling in Go services caused by globals or init()?

To eliminate hidden coupling in Go services, apply dependency injection to pass dependencies explicitly through constructors instead of using globals or init(), ensuring explicit wiring, testability, and predictable lifecycle management.

What is the best way to structure Go interfaces for dependency injection?

The best way to structure Go interfaces for dependency injection is using interface-first design at the consumption site, decoupling services from concrete implementations to enable mock testing.

How do I choose a dependency injection library for my Go project?

Choose a Go dependency injection library by evaluating project size, lifecycle needs, and type-safety constraints, selecting among google/wire, uber-go/dig with fx, and samber/do for your specific service architecture.

How do I test Go services that use a DI container?

Test Go services using a DI container by mocking at the interface boundary, and when using samber/do, clone the injector and override specific providers to isolate components for testing.

When should I avoid passing the DI injector into my Go services?

Avoid passing the DI injector into Go services to prevent the service-locator anti-pattern; keep the container usage strictly at the app startup composition root and inject explicit dependencies.