golang-dependency-injection

Design Go dependency injection architecture with constructor injection and explicit wiring.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Dependency injection (DI) removes hidden dependencies by forcing Go services to receive their collaborators explicitly, making code easier to test, refactor, and maintain.

Core Features & Use Cases

  • Constructor-based wiring: Use explicit constructor injection to prevent global variables and init-driven service setup.
  • Interface-first boundaries: Define interfaces at the point of consumption to decouple services from concrete implementations.
  • Correct composition root: Keep any DI container limited to application startup, avoiding the service-locator anti-pattern (passing containers into services).
  • Practical library selection: Compare manual DI, google/wire (codegen), uber-go/dig+fx (reflection framework), and samber/do (generics-based, with lifecycle support).
  • Refactoring guidance: Provide a structured approach to untangle coupled code paths by identifying global init patterns, dependency surfaces, and service-locator misuse.
  • Testing with mocks: Inject mock implementations at interface boundaries to test business logic without real infrastructure.

Quick Start

Ask the AI to refactor your Go service to use constructor injection, then choose between manual DI, google/wire, dig+fx, or samber/do based on your service count 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 use dependency injection in Go to make services testable?

Dependency injection in Go makes services testable by replacing hidden collaborators with explicit constructor wiring, allowing you to inject mock implementations at interface boundaries for isolated business logic testing.

What is the best way to choose between google/wire, dig+fx, and samber/do for Go dependency injection?

Choose between google/wire, dig+fx, and samber/do by assessing your service count and lifecycle needs: wire offers compile-time safety, fx provides runtime reflection, and samber/do adds generics-based lifecycle support.

How do I refactor coupled Go packages to use constructor injection?

Refactor coupled Go packages to constructor injection by identifying global init patterns and dependency surfaces, replacing hidden global state with explicitly wired constructors passed through a central composition root.

Why should I avoid the service-locator anti-pattern when using a DI container in Go?

Avoid the service-locator anti-pattern in Go dependency injection by restricting container usage exclusively to the application startup composition root, never passing the container itself into services.

Do I need to define interfaces at the consumer or provider for Go dependency injection?

Define interfaces at the point of consumption in Go dependency injection to decouple consumer services from concrete implementations, ensuring explicit boundaries that simplify mocking and architectural testing.

How do I inject mock implementations to test Go services without real databases?

Inject mock implementations into Go services by defining interfaces at the consumer boundary and passing test doubles via constructor injection, isolating business logic from real databases and stateful clients.