Back to Skills

golang-uber-dig

Implements dependency injection in Golang using uber-go/dig — reflection-based container, Provide/Invoke, dig.In/dig.Out parameter and result objects, named values, value groups, optional dependencies, scopes, and Decorate. Apply when using or adopting uber-go/dig, when the codebase imports `go.uber.org/dig`, or when wiring an application graph at startup. For higher-level lifecycle and modules, see `samber/cc-skills-golang@golang-uber-fx` skill.

2,164stars137forksUpdated 6/16/2026

Security Assessment

Safe(90/100)

Detected risks:

Sensitive File Access([references/recipes.md] .Env)
Security Score90/100

About golang-uber-dig

The golang-uber-dig skill provides expert guidance for implementing dependency injection in Go applications using the uber-go/dig library. This reflection-based container manages object graphs at application startup, enabling developers to wire complex service dependencies without manual instantiation. The skill helps teams adopt dig's core primitives including Provide/Invoke patterns, parameter and result objects using dig.In/dig.Out, and advanced features like named values, value groups, optional dependencies, scopes, and decorators.

The skill covers dig's lazy and memoized constructor system where each type is built once and shared as a singleton per container. It explains how to structure constructors following the "accept interfaces, return structs" principle, use parameter objects to manage functions with multiple dependencies, leverage named values and value groups for disambiguation, handle optional dependencies gracefully, and apply decorators to modify provided values. The skill also clarifies the relationship between dig and fx, helping developers choose the appropriate tool based on whether they need just dependency injection or a full application framework with lifecycle management.

This skill targets Go architects and backend engineers building services with complex dependency graphs, particularly those migrating to or already using uber-go/dig. It's especially valuable when wiring application composition roots, building CLI tools or libraries that expose a DI container, creating test harnesses, or embedding dependency injection into existing applications. The skill ensures developers follow best practices like keeping the container at the composition root, depending on interfaces rather than concrete types, and treating constructor errors as first-class failures.

FAQ

When should I use dig instead of fx?

Use dig when you only need dependency injection without lifecycle management: CLI tools, libraries exposing a container to callers, test harnesses, or embedding DI into existing apps that manage their own lifecycle. Use fx for long-running services like HTTP servers or workers that need lifecycle hooks, signal handling, and structured startup/shutdown.

How does dig handle constructor execution and dependencies?

Dig constructors are lazy and memoized. They only execute when their output is needed, and each type is built once and shared as a singleton per container. Provide registers constructors without executing them, while Invoke triggers execution by requesting dependencies as function parameters.

What are dig.In and dig.Out used for?

dig.In and dig.Out are embedded structs used to create parameter and result objects. Use dig.In when a constructor has 4+ dependencies to group them into a single struct parameter. Use dig.Out when returning multiple values from a constructor, allowing you to name outputs and leverage features like value groups.

What Go version and tools does this skill require?

The skill requires the Go toolchain (go binary) and optionally golangci-lint for linting. It's designed for projects importing go.uber.org/dig and works with Claude Code or similar AI coding agents. Install dig with: go get go.uber.org/dig

Can I use named values or optional dependencies with dig?

Yes, dig supports named values for disambiguating multiple instances of the same type, and optional dependencies that allow constructors to handle missing dependencies gracefully. These features are accessed through struct tags on dig.In parameter objects.

All Files

5 files
references/advanced.md4.5 KB
View
references/recipes.md6.6 KB
View
SKILL.md9.9 KB
View
evals/evals.json12.1 KB
View
references/testing.md3.5 KB
View

Install golang-uber-dig

Download and extract the skill files to your .claude/skills/ directory.

Quick Setup:

  1. Copy the skill folder to .claude/skills/
  2. Claude will automatically detect and use the skill