Wire for Go: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Wire and why is it needed?

\n\n

Wire is a dependency injection (DI) library for the Go language, developed by the Google team. Unlike many other DI frameworks that work at runtime, Wire uses code generation at compile time. This is a key difference: Wire analyzes your code and generates a wire_gen.go file that contains all the dependency wiring logic. This approach eliminates errors related to reflection (reflect) and makes the code fully typed.

\n\n

Why is this needed?

\n
    \n
  • Simplified configuration: Instead of manually creating all dependencies in main.go, Wire does it automatically.
  • \n
  • Testing: You can easily replace real implementations with mocks for unit tests.
  • \n
  • Clean code: The code becomes more modular and less coupled (low coupling).
  • \n
  • Type safety: Configuration errors are detected at compile time, not at runtime.
  • \n
\n\n

Wire is ideal for large projects where the number of dependencies runs into the tens or hundreds.

\n\n

Installation

\n\n

Wire is not a library in the traditional sense (like a package you import). It is a command-line tool (CLI). Installation is done via the standard Go package manager:

\n\n
go install github.com/google/wire/cmd/wire@latest
\n\n

After executing this command, the wire binary will be available in your $GOPATH/bin (or $GOBIN). Make sure this directory is added to the PATH environment variable.

\n\n

Verify the installation:

\n\n
wire help
\n\n

If you see the command help — everything is ready to go.

\n\n

Quick start with code example

\n\n

Let's create a simple application that greets the user. We will have three components: Message, Greeter, and Event.

\n\n

Step 1: Project structure

\n\n
myapp/\n├── main.go\n├── wire.go\n└── wire_gen.go  (will be generated)
\n\n

Step 2: Create components (main.go)

\n\n
package main\n\nimport "fmt"\n\n// Message — a simple structure containing text.\ntype Message struct {\n    Text string\n}\n\n// NewMessage — constructor for Message.\nfunc NewMessage(text string) Message {\n    return Message{Text: text}\n}\n\n// Greeter — a structure that greets using Message.\ntype Greeter struct {\n    Message Message\n}\n\n// NewGreeter — constructor for Greeter.\nfunc NewGreeter(m Message) Greeter {\n    return Greeter{Message: m}\n}\n\n// Greet — method returning a greeting.\nfunc (g Greeter) Greet() Message {\n    return g.Message\n}\n\n// Event — a structure representing an event.\ntype Event struct {\n    Greeter Greeter\n}\n\n// NewEvent — constructor for Event.\nfunc NewEvent(g Greeter) Event {\n    return Event{Greeter: g}\n}\n\n// Start — method that starts the event.\nfunc (e Event) Start() {\n    msg := e.Greeter.Greet()\n    fmt.Println(msg.Text)\n}\n\nfunc main() {\n    // Manual dependency creation (without Wire)\n    message := NewMessage("Hello, Wire!")\n    greeter := NewGreeter(message)\n    event := NewEvent(greeter)\n\n    event.Start()\n}
\n\n

Step 3: Create the wire.go file

\n\n

This file is the "provider" for Wire. It contains initializer functions (providers) and a function that tells Wire what to assemble.

\n\n
//go:build wireinject\n// +build wireinject\n\npackage main\n\nimport "github.com/google/wire"\n\n// InitializeEvent — function that Wire will use to generate code.\n// It declares that we need an Event, and Wire will build the dependency graph itself.\nfunc InitializeEvent(text string) Event {\n    w                    

Recommendations