golang-dependency-injection

Guide Go dependency injection with constructor injection and library comparisons.

2.9k|191|Updated Mar 21, 2026
One-click install
npx skills add https://github.com/samber/cc-skills-golang --skill golang-dependency-injection
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-dependency-injection
Source: https://github.com/samber/cc-skills-golang/tree/main/skills/golang-dependency-injection
Command: npx skills add https://github.com/samber/cc-skills-golang --skill golang-dependency-injection

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Provides structured guidance on implementing Dependency Injection (DI) in Go, including manual constructor injection, library comparisons, and best practices to avoid global state and init-time wiring.

Core Features & Use Cases

  • Understand when to use manual DI vs libraries like google/wire, uber-go/dig, uber-go/fx, and samber/do.
  • Learn design principles for testable, loosely coupled Go applications and strategies for lifecycle and mockability.
  • Compare DI approaches across project sizes, with practical examples and decision guidance.

Quick Start

Wire up a small Go app in the main function by wiring infrastructure, repositories, and services with manual DI, then migrate to a DI library as the project grows.

Frequently Asked Questions about golang-dependency-injection

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

FAQPage Schema
What is the best way to do dependency injection in Go for testable code?

Dependency injection in Go is best implemented by wiring infrastructure, repositories, and services via manual constructor injection in the main function, then migrating to a DI library like google/wire or samber/do as the project grows to ensure testable architectures.

How do I choose between google/wire, uber-go/dig, uber-go/fx, and samber/do?

Choosing between google/wire, uber-go/dig, uber-go/fx, and samber/do requires comparing DI approaches across project sizes, evaluating code generation versus runtime reflection, and assessing lifecycle management needs for your specific microservices architecture.

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

You should use a dependency injection library instead of manual wiring when your Go project grows from a small service to a large microservices architecture, requiring automated lifecycle management and complex mockability for testing.

How do I structure a Go application to avoid global state and init-time wiring?

To avoid global state and init-time wiring in Go applications, structure your code using constructor injection within the main function, passing dependencies explicitly to achieve loosely coupled, testable components.

Does dependency injection work well for large Go microservices?

Dependency injection works well for large Go microservices by providing best-practice guidance for testable architectures, supporting complex constructor injection, lifecycle considerations, and scalable mockability across growing project sizes.