Skip to content

goforj/wire

Repository files navigation

goforj/wire logo

Compile-time dependency injection for Go - fast, explicit, and reflection-free.

Go Reference License Go Test Go version Latest tag Go Report Card

wire generates plain Go code to wire your application together. No runtime container, no reflection, no hidden magic - just fast, explicit initialization.

Note

This is a maintained fork of google/wire. The original project is no longer actively maintained. This fork preserves Wire’s API and behavior while focusing on:

  • 8-10x+ Faster compile times
  • Improved generator determinism
  • Better documentation and developer ergonomics (wire watch etc.)

Existing Wire codebases should work without modification.

Wire is a code generation tool that automates connecting components using dependency injection. Dependencies between components are represented in Wire as function parameters, encouraging explicit initialization instead of global variables. Because Wire operates without runtime state or reflection, code written to be used with Wire is useful even for hand-written initialization.

For an overview, see the introductory blog post.

Installing

go install github.com/goforj/wire/cmd/wire@latest

Ensure $GOPATH/bin is in your $PATH.

Compatibility with google/wire

Wire remains compatible with codebases that import github.com/google/wire. The generator emits a //go:generate directive pointing at this fork so go generate consistently uses the maintained toolchain.

To keep existing imports unchanged:

go mod edit -replace=github.com/google/wire=github.com/goforj/wire@latest

How Wire Works (in 60 seconds)

Wire is a compile-time dependency injection tool. Instead of building a runtime container, Wire generates ordinary Go code that explicitly initializes your application.

There are only two concepts to learn:

Providers

A provider is a normal Go function that produces a value (and optionally an error).

func NewDB(cfg Config) (*sql.DB, error) {
    // ...
}

Dependencies are expressed as function parameters - no globals, no hidden state.

Injectors

An injector is a stub function whose body calls wire.Build(...). Wire replaces this stub with generated code that calls providers in dependency order.

func InitializeApp(cfg Config) (*App, error) {
    wire.Build(AppSet)
    return nil, nil
}

The generated output is plain Go - readable, debuggable, and fast.

Minimal Example

// providers.go
type Message string

func ProvideMessage() Message {
    return "hello wire"
}

type Greeter struct {
    Msg Message
}

func NewGreeter(msg Message) *Greeter {
    return &Greeter{Msg: msg}
}

var GreeterSet = wire.NewSet(
    ProvideMessage,
    NewGreeter,
)
// wire.go
//go:build wireinject

func InitializeGreeter() *Greeter {
    wire.Build(GreeterSet)
    return nil
}

Generate the injector:

wire

Wire produces wire_gen.go containing explicit initialization code with no runtime dependency on Wire.

Running generation manually

Run the default command (alias for wire gen) or specify packages:

wire
wire gen ./...

Watching for changes

Wire includes a native watcher that re-runs generation on Go file changes:

wire watch
wire watch ./...

Detects the package root automatically and uses native filesystem notifications when available (with a polling fallback).

Design Guidance

Wire favors explicitness and long-term maintainability:

  • Prefer small provider sets, especially in libraries
  • Avoid injecting common types like string; define distinct types instead
  • Treat provider sets as public APIs with compatibility guarantees
  • Use struct providers and interface bindings to keep graphs readable

For detailed guidance, see Best Practices.

Documentation

If you're new to Wire, we recommend reading in this order:

  1. Tutorial - A guided introduction with a complete working example
  2. User Guide - Full reference for providers, injectors, bindings, and advanced patterns
  3. Best Practices - Design guidance for large codebases and libraries
  4. FAQ - Design rationale and common questions

When Not to Use Wire

Wire is designed for medium-to-large Go applications with non-trivial initialization logic.

For small programs with only a handful of dependencies, hand-written initialization is often simpler and clearer.

Project Status

This fork tracks the original Wire feature set while prioritizing performance, determinism, and documentation. Compatibility with existing Wire codebases is a core goal.

Community

For questions and discussion, use GitHub Discussions.

This project follows the Go Code of Conduct.