$npx -y skills add samber/cc-skills-golang --skill golang-google-wireCompile-time dependency injection in Golang using google/wire — wire.NewSet, wire.Build, wire.Bind (interface→concrete), wire.Struct, wire.Value, wire.InterfaceValue, wire.FieldsOf, cleanup functions, //go:build wireinject injector files, and generated wire_gen.go. Apply when usi
| 1 | **Persona:** You are a Go architect using wire for compile-time DI. You let the compiler catch missing dependencies, treat `wire_gen.go` as committed source, and re-run `wire ./...` after every graph change. |
| 2 | |
| 3 | **Dependencies:** |
| 4 | |
| 5 | - wire: `go install github.com/google/wire/cmd/wire@latest` |
| 6 | |
| 7 | # Using google/wire for Compile-Time Dependency Injection in Go |
| 8 | |
| 9 | Code-generation DI toolkit. Wire resolves the dependency graph at compile time and emits plain Go constructor calls — no runtime container, no reflection. Errors appear when you run `wire ./...`, not at first request. |
| 10 | |
| 11 | Note: `google/wire` was archived in August 2025 (feature-complete; bug fixes still accepted). |
| 12 | |
| 13 | **Official Resources:** [pkg.go.dev](https://pkg.go.dev/github.com/google/wire) · [github.com/google/wire](https://github.com/google/wire) · [User Guide](https://github.com/google/wire/blob/main/docs/guide.md) · [Best Practices](https://github.com/google/wire/blob/main/docs/best-practices.md) |
| 14 | |
| 15 | 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. |
| 16 | |
| 17 | ```bash |
| 18 | go get -tool github.com/google/wire/cmd/wire@latest |
| 19 | go get github.com/google/wire |
| 20 | ``` |
| 21 | |
| 22 | ## wire vs. Runtime DI |
| 23 | |
| 24 | | Concern | wire | dig / fx / samber/do | |
| 25 | | ----------------- | ------------------------- | ---------------------- | |
| 26 | | Resolution | Compile time (codegen) | Runtime (reflection) | |
| 27 | | Error detection | `wire ./...` fails | First `Invoke`/startup | |
| 28 | | Runtime container | None — plain Go calls | Present | |
| 29 | | Lifecycle hooks | Not built in | fx: OnStart/OnStop | |
| 30 | | Generated files | `wire_gen.go` (committed) | None | |
| 31 | |
| 32 | For lifecycle, lazy loading, and a full matrix see `samber/cc-skills-golang@golang-dependency-injection`. |
| 33 | |
| 34 | ## Providers |
| 35 | |
| 36 | A provider is any Go function — inputs are dependencies, outputs are provided types. Three return forms: |
| 37 | |
| 38 | ```go |
| 39 | func NewConfig() *Config { return &Config{Addr: ":8080"} } |
| 40 | func NewDB(cfg *Config) (*sql.DB, error) { return sql.Open("postgres", cfg.DSN) } |
| 41 | func NewRedis(cfg *Config) (*redis.Client, func(), error) { // cleanup chained in reverse order |
| 42 | c := redis.NewClient(&redis.Options{Addr: cfg.RedisAddr}) |
| 43 | return c, func() { c.Close() }, nil |
| 44 | } |
| 45 | ``` |
| 46 | |
| 47 | ## Provider Sets |
| 48 | |
| 49 | `wire.NewSet` groups providers for reuse. Sets can reference other sets. |
| 50 | |
| 51 | ```go |
| 52 | // infra/wire.go |
| 53 | var InfraSet = wire.NewSet( |
| 54 | NewConfig, |
| 55 | NewDB, |
| 56 | NewRedis, |
| 57 | ) |
| 58 | |
| 59 | // service/wire.go |
| 60 | var ServiceSet = wire.NewSet( |
| 61 | NewUserRepo, |
| 62 | NewUserService, |
| 63 | wire.Bind(new(UserStore), new(*UserRepo)), // interface binding |
| 64 | ) |
| 65 | ``` |
| 66 | |
| 67 | Keep sets small: library sets expose a stable surface (adding inputs or removing outputs breaks downstream injectors). One set per package is a useful default. |
| 68 | |
| 69 | ## Injectors and `//go:build wireinject` |
| 70 | |
| 71 | The injector file declares the initialization function. Wire generates its body into `wire_gen.go` and replaces the stub. |
| 72 | |
| 73 | ```go |
| 74 | //go:build wireinject |
| 75 | |
| 76 | package main |
| 77 | |
| 78 | import "github.com/google/wire" |
| 79 | |
| 80 | // Wire generates the body of this function. |
| 81 | func InitApp() (*App, func(), error) { |
| 82 | wire.Build(InfraSet, ServiceSet, NewApp) |
| 83 | return nil, nil, nil // replaced by codegen |
| 84 | } |
| 85 | ``` |
| 86 | |
| 87 | The `//go:build wireinject` tag prevents the stub from being compiled into the binar |