golang-dependency-injection

Design dependency injection in Go using constructor injection and consumer-defined interfaces.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Dependency injection in Go removes hidden dependencies created by constructors, globals, and init-time wiring, making services easier to test, refactor, and evolve.

Core Features & Use Cases

  • Constructor-based wiring: Enforces passing dependencies explicitly into constructors to keep services loosely coupled.
  • Interface-at-the-consumer boundary: Guides where interfaces should be defined so implementations can vary without breaking consumers.
  • Library selection for lifecycle needs: Helps choose between manual injection, google/wire, uber-go/dig+fx, and samber/do based on project size and lifecycle requirements.
  • Refactoring support: Provides a structured approach to untangle existing tightly coupled Go code, including anti-pattern detection like passing containers into services.

Quick Start

Ask for a dependency-injected design and wiring plan in Go that replaces globals and init() with constructor parameters, and includes either manual wiring for small services or samber/do for larger systems with lifecycle management.

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 improve service testability?

Dependency injection in Go improves service testability by enforcing explicit constructor injection, eliminating hidden coupling from globals and init() so dependencies can be easily mocked and refactored.

What is the best way to structure Go constructors to avoid hidden dependencies?

The best way to structure Go constructors is through explicit constructor injection, passing dependencies as parameters rather than relying on globals, init() functions, or service-locator patterns.

How do I refactor existing tightly coupled Go code to use dependency injection?

Refactoring tightly coupled Go code to use dependency injection involves replacing globals and init() with constructor parameters, defining consumer interfaces, and establishing a composition-root container.

Should I use manual wiring or a dependency injection library like google/wire or uber-go/dig in Go?

Choose manual wiring for small Go services, or select a dependency injection library like google/wire, uber-go/dig+fx, or samber/do for larger systems requiring automated wiring and service lifecycle management.

Where should I define interfaces when implementing dependency injection in Go?

Define interfaces at the consumer boundary when implementing dependency injection in Go, ensuring implementations can vary without breaking consumers, which maintains loose coupling and enhances testability.

What are common anti-patterns when using a dependency injection container in Go?

A common anti-pattern when using dependency injection containers in Go is passing the container itself into services, which recreates hidden coupling and defeats the purpose of explicit constructor injection.