How to Use Wire for Compile-Time Dependency Injection

Wire generates Go code at build time to wire dependencies, requiring the 'wire' CLI tool to run before compilation.

You cannot use Wire for compile-time dependency injection in Go because Wire is a code generator that runs at build time, not a runtime library. Wire generates a wire_gen.go file containing the dependency graph logic, which the Go compiler then compiles into your binary. To use it, install the tool, define your provider and injector functions, and run the generator.

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

This command scans your wire.go file for //go:buildwire directives and generates the necessary wiring code.