golang-dependency-injection

Guides dependency injection design in Go using manual constructors or DI libraries.

1|Updated Jun 2, 2026
One-click install
npx skills add https://github.com/VerifiedOrganic/onboard --skill golang-dependency-injection-verifiedorganic
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: golang-dependency-injection
Source: https://github.com/VerifiedOrganic/onboard/tree/main/.agents/skills/golang-dependency-injection
Command: npx skills add https://github.com/VerifiedOrganic/onboard --skill golang-dependency-injection-verifiedorganic

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go services often end up with global variables, init() setup, and hardcoded dependencies that make code untestable and tightly coupled. This Skill guides you to inject dependencies through constructors, define interfaces at consumption sites, and pick the right DI approach for your project size. ## Core Features & Use Cases - Manual Constructor Injection: Wire small projects (< 10 services) explicitly in main() with layered initialization order, no library required. - DI Library Decision Table: Compare google/wire, uber-go/dig, uber-go/fx, and samber/do across type safety, lifecycle management, lazy loading, and learning curve. - Refactor Mode: Detect global variables, concrete-type coupling, and service-locator anti-patterns in existing code, then produce a migration plan. - Use Case: You inherit a Go microservice where every service opens its own database connection via globals. The Skill maps the dependency graph, flags the anti-patterns, and generates constructor-based wiring with mockable interfaces for tests. ## Quick Start Ask the agent to review your Go service's dependency setup and recommend whether to use manual injection or a DI library like samber/do, wire, or fx.

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 without a library?▼

Use manual constructor injection: pass dependencies as parameters to constructors like NewUserService(db, mailer, logger) and wire everything explicitly in main(). Initialize infrastructure first, then repositories, services, and transport. This is the recommended approach for projects with fewer than 10 services.

Which Go DI library should I use: wire, fx, dig, or samber/do?▼

Choose based on project needs: google/wire gives compile-time safety via code generation but no lifecycle management; uber-go/fx adds lifecycle hooks but uses reflection; samber/do offers compile-time generics safety with built-in health checks and shutdown. Small projects should skip libraries entirely.

Should I pass the DI container as a dependency to my services?▼

No, passing the container or injector into services is the service locator anti-pattern. The container must only exist at the composition root in main(). Inject the specific dependencies each service needs through its constructor instead.

Where should I define interfaces in a Go project using DI?▼

Define interfaces in the consuming package, not the implementation package. Follow the principle of accepting interfaces and returning structs: the implementation returns a concrete struct pointer, and the consumer declares the small interface it needs locally.

When does manual dependency injection break down in Go?▼

Manual DI breaks down around 15-20+ services when wiring order becomes fragile, lifecycle management must be hand-coded, and there is no lazy initialization. At that scale, adopt a library with built-in lifecycle support such as samber/do or uber-go/fx.

How do I test Go services that use dependency injection?▼

Create a mock implementing the dependency interface and inject it through the constructor in your test, avoiding real databases. With samber/do, you can clone the container and override only the services you need to mock for integration tests.