golang-dependency-injection

Guide Go developers in implementing constructor-based dependency injection.

Updated Mar 29, 2026
One-click install
npx skills add https://github.com/osmanozen/go-commerce --skill golang-dependency-injection-osmanozen
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-dependency-injection
Source: https://github.com/osmanozen/go-commerce/tree/main/.agents/skills/golang-dependency-injection
Command: npx skills add https://github.com/osmanozen/go-commerce --skill golang-dependency-injection-osmanozen

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Go projects often suffer from tight coupling, global state, and hard-to-test wiring. This Skill provides clear guidelines for dependency injection in Go, including constructor injection, avoiding init-based setup, and choosing the right DI approach for your project.

Core Features & Use Cases

  • Explicit constructor injection as the default pattern, enabling easy testing and mocking.
  • Guidance on when to use manual wiring vs. DI libraries (wire, uber/dig+fx, or samber/do) based on project size and lifecycle needs.
  • Practices to avoid the service locator anti-pattern by keeping the DI container at the composition root and injecting dependencies through interfaces.

Quick Start

Provide a small Go example that wires a UserService with a repository and logger at startup.

Frequently Asked Questions about golang-dependency-injection

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

FAQPage Schema
How do I implement dependency injection in Go to improve testability?

Dependency injection in Go is best implemented using explicit constructor injection, passing dependencies through interfaces at the composition root to enable easy mocking and testing. This approach keeps wiring explicit and avoids global state.

What is the service locator anti-pattern in Go dependency injection?

The service locator anti-pattern occurs when a DI container is passed around and used to resolve dependencies dynamically at runtime. This Skill guides keeping the container at the composition root and injecting dependencies via interfaces to avoid tight coupling.

Wire vs dig/fx vs samber/do: which Go dependency injection library should I choose?

Choosing between Wire, dig/fx, and samber/do depends on project size and lifecycle needs. This Skill provides library comparisons, guiding when to use manual wiring versus DI libraries for small services or large microservices.

Does dependency injection work with Go 1.18 and later versions?

Yes, the dependency injection guidelines and library comparisons provided are compatible with Go 1.18 and later versions. The patterns apply to architectures ranging from small services to large microservices.

When should I avoid init-based setup for Go dependency wiring?

You should avoid init-based setup when it creates hidden dependencies and global state that hinder testing. This Skill recommends explicit constructor injection at startup to ensure maintainability and clean wiring.