Esta página se muestra en inglés. Una traducción al español está en curso.
ScriptsMay 3, 2026·3 min de lectura

Wire — Compile-Time Dependency Injection for Go

A code generation tool by Google that automates wiring of dependencies at compile time, eliminating reflection-based DI containers and runtime errors.

Introduction

Wire is a dependency injection code generator for Go developed by Google. Unlike runtime DI containers that use reflection, Wire resolves dependencies entirely at compile time by generating plain Go code. This means dependency errors are caught during compilation, and the generated code is as fast as hand-written initialization.

What Wire Does

  • Generates dependency wiring code from provider function declarations
  • Resolves the full dependency graph at compile time with no reflection
  • Detects missing providers and circular dependencies as build errors
  • Supports provider sets for grouping related providers into reusable units
  • Handles cleanup functions for resources that need teardown

Architecture Overview

Wire operates in two phases. First, you declare provider functions (constructors that return a type and optionally an error or cleanup func) and group them into ProviderSets. Then you write an injector function signature with a wire.Build call listing the needed sets. When you run the wire command, it analyzes the dependency graph, topologically sorts providers, and generates a Go source file that calls each provider in order, passing return values as arguments to downstream providers.

Self-Hosting & Configuration

  • Install: go install github.com/google/wire/cmd/wire@latest
  • Write provider functions that accept dependencies as parameters and return a type
  • Group providers into wire.NewSet() for organizational reuse
  • Write an injector function in a wire.go file with a //go:build wireinject tag
  • Run wire ./... to generate wire_gen.go with the resolved wiring code

Key Features

  • Zero runtime overhead: generated code is plain function calls with no reflection
  • Compile-time safety catches missing or ambiguous providers before the program runs
  • Provider sets enable modular organization and reuse across packages
  • Supports interfaces via binding declarations (wire.Bind)
  • Cleanup function propagation ensures resources are properly released

Comparison with Similar Tools

  • Uber Fx — runtime DI container using reflection; Wire generates code at compile time with no runtime cost
  • dig (Uber) — reflection-based container underlying Fx; Wire trades flexibility for compile-time safety
  • do — lightweight DI using generics (Go 1.18+); Wire predates generics and uses code generation
  • inject (facebookgo) — struct-tag-based injection; Wire uses explicit provider functions for clarity
  • manual wiring — Wire automates exactly what you would write by hand, reducing boilerplate in large apps

FAQ

Q: Do I commit the generated wire_gen.go file? A: Yes. The generated file should be committed so that builds work without requiring the wire tool.

Q: Can Wire handle interfaces? A: Yes. Use wire.Bind to declare that a concrete type satisfies an interface, and Wire will resolve it.

Q: Does Wire support optional dependencies? A: Not directly. You can use wire.Value to provide zero-value defaults or wrap optional logic in a provider.

Q: How does Wire handle errors from providers? A: If a provider returns an error, Wire generates code that checks it and returns early, propagating the error to the injector caller.

Sources

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados