golang-dependency-injection

Design dependency injection wiring for Go applications using constructor-based composition.

2|Updated Mar 13, 2023
One-click install
npx skills add https://github.com/haipham22/golang-sample --skill golang-dependency-injection-haipham22
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-dependency-injection
Source: https://github.com/haipham22/golang-sample/tree/main/.agents/skills/golang-dependency-injection
Command: npx skills add https://github.com/haipham22/golang-sample --skill golang-dependency-injection-haipham22

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps you design testable, loosely coupled Go services by replacing globals, hidden setup, and container misuse with explicit dependency injection.

Core Features & Use Cases

  • Manual constructor injection for small Go projects, with dependencies passed through constructors and wired in main.
  • DI pattern selection across manual DI, google/wire, uber-go/dig, uber-go/fx, and samber/do based on size, lifecycle, and team needs.
  • Architecture guidance for consumer-defined interfaces, shallow dependency graphs, lazy initialization, and composition-root-only container usage.
  • Use Case: Refactor a REST API so repositories, services, and handlers are composed cleanly, then test business logic with mocks instead of a real database.

Quick Start

Ask for a Go dependency injection plan that recommends the simplest wiring approach for my project and shows the constructors and dependency order.

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 Golang to make services testable?

Dependency injection in Golang replaces globals and hidden setup with explicit constructor-based wiring, allowing you to test business logic with mocks instead of real databases. You pass dependencies through constructors and compose them cleanly at the application's composition root.

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

Choosing between google/wire, uber-go/fx, uber-go/dig, and samber/do depends on your project's size, lifecycle requirements, and team needs. Manual constructor injection is recommended for small projects, while larger applications may benefit from code generation or runtime containers.

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

To refactor tightly coupled Go code, define consumer-defined interfaces and establish shallow dependency graphs. Pass dependencies through constructors instead of using globals, wiring them together explicitly in your main function to ensure loose coupling.

When should I limit dependency container usage in a Go application?

You should limit dependency container usage strictly to the composition root in Go applications. Restricting container access to the entry point prevents container misuse and maintains explicit dependency wiring throughout your service architecture.

Does google/wire support lazy initialization for Go dependency injection?

Yes, dependency injection frameworks like google/wire and uber-go/fx support lazy initialization. Architecture guidance for Go DI recommends lazy initialization and shallow dependency graphs to manage service lifecycles efficiently.

Why does dependency injection require consumer-defined interfaces in Go?

Dependency injection requires consumer-defined interfaces in Go to ensure loose coupling and testability. By defining interfaces where dependencies are consumed rather than where they are implemented, you can easily inject mocks and isolate business logic for testing.