# Golang Uber Dig > Implementa inyección de dependencias en Golang con uber-go/dig: contenedor basado en reflexión, Provide/Invoke, dig.In/dig.Out, valores nombrados, value groups, dependencias opcionales, scopes y Decorate. Fuente: https://skillsagentes.com/skills/samber/cc-skills-golang/golang-uber-dig Markdown: https://skillsagentes.com/skills/samber/cc-skills-golang/golang-uber-dig.md Repositorio: https://github.com/samber/cc-skills-golang Autor: samber Licencia: MIT Actualizado: el mes pasado Coste de contexto: 113 tok instalada, 2.7k tok al activarse, 9.5k tok con todos los archivos del bundle Bundle: 5 archivos, 37 KB Permisos que pide: read edit write glob grep bash(go:*) bash(golangci-lint:*) bash(git:*) agent webfetch mcp__context7__resolve-library-id mcp__context7__query-docs bash(godig:*) bash(gopls:*) lsp mcp__gopls__* ## Instalación Un skill son archivos markdown: los mismos archivos valen para cualquier agente y lo único que cambia es el directorio de destino, es decir la bandera `--agent`. Añade `-g` para instalarlo en todos los proyectos de la máquina. ```bash # Claude Code npx -y skills add samber/cc-skills-golang --skill golang-uber-dig --agent claude-code # Cursor npx -y skills add samber/cc-skills-golang --skill golang-uber-dig --agent cursor # Codex npx -y skills add samber/cc-skills-golang --skill golang-uber-dig --agent codex # Gemini CLI npx -y skills add samber/cc-skills-golang --skill golang-uber-dig --agent gemini # Windsurf npx -y skills add samber/cc-skills-golang --skill golang-uber-dig --agent windsurf # Cline npx -y skills add samber/cc-skills-golang --skill golang-uber-dig --agent cline ``` ## Qué hace - Configura y usa el contenedor de dig.New() con Provide/Invoke para wiring reflexivo - Aplica dig.In/dig.Out para objetos de parámetros y resultados, valores nombrados y value groups - Expone constructores concretos como interfaces con dig.As y decoradores con Decorate/Scopes - Aplica buenas prácticas: composition root único, interfaces en vez de tipos concretos, validación eager en tests ## Cuándo usarla - Al usar o adoptar uber-go/dig - Cuando el código importa `go.uber.org/dig` - Al conectar (wire) el grafo de una aplicación en el arranque ## Cuándo no - Para lifecycle de aplicación, módulos y Run() con señales — usar `golang-uber-fx` - Para DI basada en generics sin reflexión — usar `golang-samber-do` - Para DI en tiempo de compilación sin contenedor en runtime — usar `golang-google-wire` ## Qué la activa - "Ayúdame a wirear mi app en Go con uber-go/dig" - "Cómo uso dig.In y dig.Out para mis constructores" - "Tengo dos proveedores del mismo tipo en dig, ¿cómo los distingo con Name?" - "Explícame value groups y dig.As en dig" ## Antes de instalar - Requiere el binario `go` y el módulo `go.uber.org/dig` instalado (`go get go.uber.org/dig`). ## Archivos - SKILL.md — 10 KB - evals/evals.json — 12 KB - references/advanced.md — 5 KB - references/recipes.md — 7 KB - references/testing.md — 4 KB ## SKILL.md Reproducido tal cual desde samber/cc-skills-golang bajo MIT. Esta sección es el documento original y está en inglés. **Persona:** You are a Go architect wiring an application graph with dig. You keep the container at the composition root, depend on interfaces not concrete types, and treat constructor errors as first-class failures. # Using uber-go/dig for Dependency Injection in Go Reflection-based DI toolkit, designed to power application frameworks (it is the engine behind `uber-go/fx`) and resolve object graphs during startup. **Official Resources:** - [pkg.go.dev/go.uber.org/dig](https://pkg.go.dev/go.uber.org/dig) - [github.com/uber-go/dig](https://github.com/uber-go/dig) This skill is not exhaustive. Please refer to library documentation and code examples for more information. For Go package docs, symbols, versions, importers, and known vulnerabilities, → See `samber/cc-skills-golang@golang-pkg-go-dev` skill (`godig`) — prefer it over Context7 for Go package facts. To navigate this library's usage in your own code (definitions, call sites, diagnostics), → See `samber/cc-skills-golang@golang-gopls` skill (`gopls`). Context7 remains a fallback for docs not indexed on pkg.go.dev. ```bash go get go.uber.org/dig ``` ## dig vs. fx fx is built on dig and shares the same container engine — the DI primitives (`Provide`, `Invoke`, `In`/`Out` structs, named values, value groups) are identical. `fx.In`/`fx.Out` are re-exports of `dig.In`/`dig.Out`. What fx adds on top of dig: | Concern | dig | fx | | --- | --- | --- | | DI container | ✅ `dig.New()` | ✅ (embedded) | | Lifecycle hooks | ❌ | ✅ `fx.Lifecycle` OnStart/OnStop | | Module system | ❌ | ✅ `fx.Module` with scoped decorators | | Signal-aware run loop | ❌ | ✅ `app.Run()` blocks on SIGINT/SIGTERM | | Structured event logging | ❌ | ✅ `fx.WithLogger` / `fxevent` | | Startup/shutdown timeout | ❌ | ✅ `fx.StartTimeout` / `fx.StopTimeout` | **Choose dig** when you need the wiring graph only: CLI tools, libraries exposing a container to callers, test harnesses, or embedding DI into an existing app that manages its own lifecycle. **Choose fx** for long-running services (HTTP servers, workers, daemons) — lifecycle and signal handling are non-negotiable there. See `samber/cc-skills-golang@golang-uber-fx` skill. ## Container ```go import "go.uber.org/dig" c := dig.New() ``` Useful options: `dig.DeferAcyclicVerification()` (faster startup), `dig.RecoverFromPanics()` (turn panics into `dig.PanicError`), `dig.DryRun(true)` (validate without invoking). ## Provide and Invoke ```go // Register a constructor — lazy, only runs when its output is needed err := c.Provide(func(cfg *Config) (*sql.DB, error) { return sql.Open("postgres", cfg.DSN) }) // Pull a service out of the container by asking for it as a function parameter err = c.Invoke(func(db *sql.DB) error { return db.Ping() }) ``` Constructors are **lazy** and **memoized**: each output type is built once and shared (singleton per container). `Provide` errors at registration if the constructor is malformed; `Invoke` returns the constructor's error wrapped with the dependency path that triggered it. A dig constructor is any function. Inputs are dependencies, outputs are provided types. `error` (last return) signals construction failure. Follow "accept interfaces, return structs". ## Parameter Objects with `dig.In` Once a constructor has 4+ dependencies, embed `dig.In` to group them as struct fields and tag fields: ```go type HandlerParams struct { dig.In Logger *zap.Logger DB *sql.DB Cache *redis.Client `optional:"true"` // zero value if not provided DBRO *sql.DB `name:"readonly"` // named dependency Routes []http.Handler `group:"routes"` // value group } func NewHandler(p HandlerParams) *Handler { /* ... */ } ``` Tags: `name:"..."`, `optional:"true"`, `group:"..."`. ## Result Objects with `dig.Out` Return several values from one constructor and attach `name`/`group` tags to results: ```go type ConnResult struct { dig.Out ReadWrite *sql.DB `name:"primary"` ReadOnly *sql.DB `name:"readonly"` } func NewConnections(cfg *Config) (ConnResult, error) { /* ... */ } ``` ## Named Values Two providers of the same type collide. Disambiguate with `dig.Name`: ```go c.Provide(NewPrimaryDB, dig.Name("primary")) c.Provide(NewReadOnlyDB, dig.Name("readonly")) ``` Consume by adding `name:"primary"` / `name:"readonly"` to a `dig.In` field. ## Value Groups Many providers, one consumer slice — typical for HTTP handlers, health checks, migrations: ```go type RouteResult struct { dig.Out Handler http.Handler `group:"routes"` } func NewUserHandler(db *sql.DB) RouteResult { /* ... */ } func NewPostHandler(db *sql.DB) RouteResult { /* ... */ } type ServerParams struct { dig.In Routes []http.Handler `group:"routes"` } ``` **Flatten** — append `,flatten` (e.g. `group:"routes,flatten"`) to unwrap a slice instead of nesting it. Group order is **not guaranteed**; if order matters, provide an explicit ordered slice from a single constructor. ## Provide as Interface (`dig.As`) Register a concrete constructor and expose it under one or more interfaces without a separate adapter: ```go c.Provide(NewPostgresDB, dig.As(new(Database), new(io.Closer))) // Consumers ask for Database or io.Closer; *PostgresDB stays hidden. ``` ## Full Application Example ```go func main() { c := dig.New() must(c.Provide(NewConfig)) must(c.Provide(NewLogger)) must(c.Provide(NewDatabase)) must(c.Provide(NewServer)) err := c.Invoke(func(srv *http.Server) error { return srv.ListenAndServe() }) if err != nil { log.Fatal(err) } } func must(err error) { if err != nil { panic(err) } } ``` dig has **no built-in lifecycle**. If you need OnStart/OnStop hooks, signal handling, and graceful shutdown, use fx — see `samber/cc-skills-golang@golang-uber-fx` skill. For Decorate, Scopes, optional deps, error helpers, and Visualize, see [advanced.md](./references/advanced.md). ## Best Practices 1. Keep the container at the composition root — never pass `*dig.Container` as a parameter; treat it like a plumbing detail of `main()`. Service-locator patterns defeat the testability gains of DI. 2. Depend on interfaces, not concrete types — lets you swap implementations in tests without touching production code, and lets you use `dig.As` to expose narrow interfaces from wide structs. 3. Prefer parameter objects (`dig.In` structs) once a constructor has 4+ dependencies — call sites stay readable and adding a new dependency is a one-line change instead of a signature break. 4. Group registration by module (one file per module that calls `c.Provide` for its types) — review and refactoring become a per-module concern, and you can extract a module into a fx.Module later without rewriting wiring. 5. Validate the graph eagerly in tests — call `c.Invoke` against the composition root in CI to surface missing providers at boot time, not at first request. `DryRun(true)` skips constructor execution. 6. Return errors from constructors instead of panicking — dig wraps them with the dependency path, which makes the failure point obvious. ## Common Mistakes | Mistake | Fix | | --- | --- | | Passing the container into services | The container belongs to `main()`. Inject the typed dependencies a service needs; otherwise tests need to build a real container. | | Two providers for the same type without `Name` | dig errors at `Provide` time. Either name them, or merge into a single provider that returns a `dig.Out` result struct. | | Ignoring `Provide` errors | Wrap each `Provide` with a `must` helper. A silent registration error becomes a missing-type error far later. | | Using groups when ordering matters | Groups are unordered. If order matters (middleware chain, migration sequence), provide an explicit ordered slice with one constructor. | | Constructors with side effects on import | Keep `init()` empty — start work only inside the constructor, after the graph is built. | ## Testing dig containers are cheap — build a fresh one per test, override providers with `Decorate`, and call `Invoke` to drive the system. For full patterns (per-test wiring, shared helpers, graph validation in CI, asserting wire-time errors, recovering from constructor panics), see [testing.md](./references/testing.md). ## Further Reading - [advanced.md](./references/advanced.md) — Decorate, Scopes, optional deps, error helpers, Visualize, full Quick Reference - [recipes.md](./references/recipes.md) — end-to-end examples: HTTP server with route group, two databases, request scopes, decorators, dry-run validation - [testing.md](./references/testing.md) — testing patterns and graph validation ## Cross-References - → See `samber/cc-skills-golang@golang-uber-fx` skill for application lifecycle, modules, and signal-aware Run() built on top of dig - → See `samber/cc-skills-golang@golang-dependency-injection` skill for DI concepts and library comparison - → See `samber/cc-skills-golang@golang-samber-do` skill for a generics-based alternative without reflection - → See `samber/cc-skills-golang@golang-google-wire` skill for compile-time DI (no runtime container) - → See `samber/cc-skills-golang@golang-structs-interfaces` skill for interface design patterns - → See `samber/cc-skills-golang@golang-testing` skill for general testing patterns If you encounter a bug or unexpected behavior in uber-go/dig, open an issue at . ## Dónde encaja - Categoría: [Desarrollo de APIs](https://skillsagentes.com/categorias/desarrollo-apis.md) — Diseña, prueba y documenta APIs HTTP y GraphQL. - Creador: [samber](https://skillsagentes.com/creators/samber.md) — 0 skills en el directorio - [Todas las skills](https://skillsagentes.com/skills.md) - [Ranking de instalaciones](https://skillsagentes.com/ranking.md) ## Otras skills del mismo repositorio - [Golang Lint](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-lint.md): Buenas prácticas de linting y configuración de golangci-lint para proyectos Golang: ejecutar linters, configurar .golangci.yml, suprimir avisos con nolint, interpretar salidas y elegir linters. - [Golang How To](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-how-to.md): Orquestador de skills de Golang, siempre activo en cualquier tarea de código, revisión, debug o setup: carga las skills más relevantes de samber/cc-skills-golang, a menudo varias a la vez. - [Golang Benchmark](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-benchmark.md): Benchmarking, profiling y medición de rendimiento en Golang: escribir y comparar benchmarks, perfilar con pprof, analizar con benchstat y detectar regresiones en CI. - [Golang Testing](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-testing.md): Tests de Golang listos para producción: table-driven, suites y mocks con testify, tests paralelos, fuzzing, fixtures, detección de fugas de goroutines con goleak, snapshot testing, cobertura, tests de integración. - [Golang Performance](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-performance.md): Patrones y metodología de optimización de rendimiento en Golang: si hay cuello de botella X, aplica el patrón Y, una vez que profiling o benchmarks ya lo identificaron. ## Skills relacionadas - [Golang Graphql](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-graphql.md): Implementa APIs GraphQL en Golang con gqlgen o graphql-go: diseño de schemas, resolvers, suscripciones e integración con servicios HTTP en Go. - [Golang Grpc](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-grpc.md): Ofrece guías de uso de gRPC, organización de protobuf y patrones listos para producción en microservicios Golang: servidores/clientes, proto files, interceptores, códigos de error, TLS/mTLS, bufconn y streaming. - [Golang Swagger](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-swagger.md): Documentación OpenAPI/Swagger en Go con swaggo/swag: anotaciones, `swag init`, integraciones con gin/echo/fiber/chi/net/http, seguridad y etiquetas de struct. - [Golang Safety](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-safety.md): Codificación defensiva en Golang para evitar panics, corrupción silenciosa de datos y bugs sutiles en tiempo de ejecución. - [Golang Samber Hot](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-samber-hot.md): Caché en memoria en Golang con samber/hot: algoritmos de expulsión (LRU, LFU, TinyLFU, S3FIFO, ARC, TwoQueue, SIEVE, FIFO), TTL, loaders, sharding y métricas Prometheus. --- Skills Agentes · [Índice de páginas en markdown](https://skillsagentes.com/sitemap.md) · [Inicio](https://skillsagentes.com/index.md)