# Go dependency injection

> Go dependency injection is an open-source agent skill for developer tools. Guides Go code toward constructor injection, explicit wiring and no global state. It is available on Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, Windsurf and Goose.

- **Kind:** Skill
- **Pricing:** Open source
- **Platforms:** Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, Windsurf, Goose
- **Website:** https://github.com/eduardo-sl/go-agent-skills/tree/main/skills/(architecture)/go-dependency-injection
- **Repository:** https://github.com/eduardo-sl/go-agent-skills
- **GitHub stars:** 90
- **License:** MIT
- **Skill name:** go-dependency-injection
- **Collection:** https://github.com/eduardo-sl/go-agent-skills
- **Categories:** [developer tools](https://ai-powered.com/categories/developer-tools)
- **Listing updated:** 2026-12-02

## About

This skill teaches an agent to structure Go code around explicit dependency passing. Services receive their collaborators through constructor parameters, and all construction happens in one composition root, usually main or a run function. It walks through removing package-level singletons from legacy code by introducing a struct and a temporary wrapper that is later deleted, and it notes that small seams can be handled by injecting plain functions instead of interfaces.

The skill also gives a decision rule for when a DI framework is worth adding. Manual wiring is the default, google/wire is preferred if code generation becomes necessary, and uber-go/fx or dig are reserved for cases that need a runtime container or lifecycle management. It stresses using only one mechanism per application. A closing checklist lets the agent verify that wiring errors surface at compile time through go build.

The skill is aimed at Go projects and expects the Go toolchain to be installed. Wire and fx are optional. Its allowed tools cover reading and editing files, searching, and running go and gofmt.

## When your agent uses it

> Dependency injection in Go: constructor injection, wiring in main, avoiding global state, and when frameworks (wire, fx, dig) earn their complexity. Use when: "dependency injection", "wire up dependencies", "inject this", "remove global state", "singleton in Go", "use google/wire", "uber fx", "make this testable". Not for: interface design (go-interface-design), directory structure (go-project-layout), test doubles and mocks (go-test-quality).

## What it can access

From a static check of the files on GitHub; nothing was run.

- [Instructions only, no scripts](https://github.com/eduardo-sl/go-agent-skills/tree/HEAD/skills/(architecture)/go-dependency-injection)
- [Asks for: Read, Edit, Write, Glob, Grep, Bash(go:*), Bash(gofmt:*)](https://github.com/eduardo-sl/go-agent-skills/blob/HEAD/skills/(architecture)/go-dependency-injection/SKILL.md)
- [License: MIT](https://github.com/eduardo-sl/go-agent-skills/tree/HEAD/skills/(architecture)/go-dependency-injection)

## Features

- **Constructor injection pattern:** Shows how to pass dependencies as explicit constructor parameters and validate them.
- **Composition root wiring:** Describes building all components in main or a run function in dependency order.
- **Removing global state:** Covers migrating package-level singletons to structs with a temporary compatibility wrapper.
- **Function-based seams:** Explains injecting small functions such as clocks or ID generators for testability.
- **DI framework guidance:** Compares manual wiring, google/wire, uber fx and dig, with a decision rule for choosing.
- **DI verification checklist:** Provides a checklist for confirming dependencies are injected and wiring compiles.

## Good for

- Refactor a Go service that uses global database variables to take its dependencies through constructors
- Wire up the dependencies of a Go HTTP server in main
- Decide whether a growing Go application should adopt google/wire or stay with manual wiring
- Make a Go package testable by removing package-level singletons

## Install

### Any agent

Download the skill folder from https://github.com/eduardo-sl/go-agent-skills and copy it into your agent's skills folder.

## FAQ

### What tools does Go dependency injection need?

It requires the Go toolchain. The wire and fx packages are optional and only needed if you choose to use them.

### Does Go dependency injection prescribe a DI framework?

No. It recommends manual wiring by default and only suggests a framework when manual wiring becomes hard to maintain, with wire preferred over fx when generation is needed.

### How does Go dependency injection handle legacy global state?

It suggests introducing a struct, keeping a deprecated package-level wrapper that delegates to one instance built in main, moving callers over, and then deleting the wrapper.

### Does Go dependency injection run scripts?

No. It is instructions only, with no scripts. This comes from a static check of the files on GitHub; nothing was run. Read the files before you install it.

### Which AI agents can use Go dependency injection?

Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, Windsurf and Goose. Each reads skills in the SKILL.md format from its own folder; the install steps on this page list them.

### What are the best alternatives to Go dependency injection?

The closest alternatives to Go dependency injection are Go design patterns, Go interface design, Go project layout, Go architecture review and Go coding standards.

## Where this comes from

- Description: from the skill's own SKILL.md.

## Alternatives

- [Go design patterns](https://ai-powered.com/skills/go-design-patterns): Go design patterns is an open-source agent skill for developer tools.
- [Go interface design](https://ai-powered.com/skills/go-interface-design): Go interface design is an open-source agent skill for developer tools.
- [Go project layout](https://ai-powered.com/skills/go-project-layout): Go project layout is an open-source agent skill for developer tools.
- [Go architecture review](https://ai-powered.com/skills/go-architecture-review): Go architecture review is an open-source agent skill for developer tools.
- [Go coding standards](https://ai-powered.com/skills/go-coding-standards): Go coding standards is an open-source agent skill for developer tools.
- [Go documentation](https://ai-powered.com/skills/go-documentation): Go documentation is an open-source agent skill for developer tools.
- [Go semantic tools](https://ai-powered.com/skills/go-semantic-tools): Go semantic tools is an open-source agent skill for developer tools.
- [Go database](https://ai-powered.com/skills/go-database): Go database is an open-source agent skill for databases.

---
Source: https://ai-powered.com/skills/go-dependency-injection
