← 返回 Skills 市场
samber

Golang Google Wire

作者 Samuel Berthe · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ✓ 安全检测通过
66
总下载
0
收藏
0
当前安装
1
版本数
在 OpenClaw 中安装
/install golang-google-wire
功能描述
Compile-time dependency injection in Golang using google/wire — wire.NewSet, wire.Build, wire.Bind (interface→concrete), wire.Struct, wire.Value, wire.Interf...
使用说明 (SKILL.md)

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.

Using google/wire for Compile-Time Dependency Injection in Go

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.

Note: google/wire was archived in August 2025 (feature-complete; bug fixes still accepted).

Official Resources: pkg.go.dev · github.com/google/wire · User Guide · Best Practices

This skill is not exhaustive. Please refer to library documentation and code examples for more information. Context7 can help as a discoverability platform.

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

wire vs. Runtime DI

Concern wire dig / fx / samber/do
Resolution Compile time (codegen) Runtime (reflection)
Error detection wire ./... fails First Invoke/startup
Runtime container None — plain Go calls Present
Lifecycle hooks Not built in fx: OnStart/OnStop
Generated files wire_gen.go (committed) None

For lifecycle, lazy loading, and a full matrix see samber/cc-skills-golang@golang-dependency-injection.

Providers

A provider is any Go function — inputs are dependencies, outputs are provided types. Three return forms:

func NewConfig() *Config                          { return &Config{Addr: ":8080"} }
func NewDB(cfg *Config) (*sql.DB, error)          { return sql.Open("postgres", cfg.DSN) }
func NewRedis(cfg *Config) (*redis.Client, func(), error) { // cleanup chained in reverse order
    c := redis.NewClient(&redis.Options{Addr: cfg.RedisAddr})
    return c, func() { c.Close() }, nil
}

Provider Sets

wire.NewSet groups providers for reuse. Sets can reference other sets.

// infra/wire.go
var InfraSet = wire.NewSet(
    NewConfig,
    NewDB,
    NewRedis,
)

// service/wire.go
var ServiceSet = wire.NewSet(
    NewUserRepo,
    NewUserService,
    wire.Bind(new(UserStore), new(*UserRepo)), // interface binding
)

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.

Injectors and //go:build wireinject

The injector file declares the initialization function. Wire generates its body into wire_gen.go and replaces the stub.

//go:build wireinject

package main

import "github.com/google/wire"

// Wire generates the body of this function.
func InitApp() (*App, func(), error) {
    wire.Build(InfraSet, ServiceSet, NewApp)
    return nil, nil, nil // replaced by codegen
}

The //go:build wireinject tag prevents the stub from being compiled into the binary — only wire_gen.go (which has no such tag) makes it through go build. Without this tag, both files define the same function, causing a compile error.

Alternative syntax when a dummy return is inconvenient:

func InitApp() (*App, func(), error) {
    panic(wire.Build(InfraSet, ServiceSet, NewApp))
}

Interface Bindings

Wire forbids implicit interface satisfaction — you must declare bindings explicitly so the graph is unambiguous when multiple types implement the same interface.

var Set = wire.NewSet(
    NewPostgresUserRepo,
    wire.Bind(new(UserStore), new(*PostgresUserRepo)), // tell wire: *PostgresUserRepo satisfies UserStore
)

Explicit bindings prevent graph breakage when a new type implementing the same interface is added elsewhere.

Struct Providers and Values

wire.Struct fills struct fields from the graph without a manual constructor. Tag fields wire:"-" to exclude them.

wire.Struct(new(Server), "Logger", "DB") // inject named fields
wire.Struct(new(Server), "*")            // inject all non-excluded fields
wire.Value(Foo{X: 42})                   // constant expression (no fn calls / channels)
wire.InterfaceValue(new(io.Reader), os.Stdin) // interface-typed literal
wire.FieldsOf(new(Config), "DSN", "Addr")    // promote struct fields as graph nodes

See advanced.md for the wire:"-" exclusion tag and wire.FieldsOf details.

Disambiguating Duplicate Types

Wire forbids two providers for the same type. Wrap the underlying type in distinct named types so each has exactly one provider:

type PrimaryDSN string
type ReplicaDSN string

Full Application Example

// wire.go — injector, excluded from binary via build tag
//go:build wireinject

package main

func InitApp() (*App, func(), error) {
    wire.Build(config.ConfigSet, infra.InfraSet, service.ServiceSet, NewApp)
    return nil, nil, nil
}

// main.go
func main() {
    app, cleanup, err := InitApp()
    if err != nil { log.Fatal(err) }
    defer cleanup()
    app.Run()
}

Wire generates wire_gen.go (plain Go, committed, DO NOT EDIT). For a full example with per-package sets, cleanup-heavy graphs, and generated output, see recipes.md.

Codegen Workflow

wire ./...           # regenerate all injectors in the module
wire check ./...     # validate graph without regenerating (fast CI check)

Run wire ./... after every constructor signature change. Add //go:generate go run github.com/google/wire/cmd/wire to injector files so go generate ./... also works. Commit wire_gen.go — it must stay in sync for CI builds.

Best Practices

  1. Never edit wire_gen.go — it is overwritten on every wire ./... run. Treat it as a build artifact that happens to be committed; source of truth is the provider and injector files.
  2. Always add //go:build wireinject to injector files — omitting it causes duplicate-symbol compile errors because both the stub and the generated file define the same function.
  3. Use named types to distinguish values of the same underlying type — wire enforces one provider per type; named types like type DSN string let you have PrimaryDSN and ReplicaDSN coexist.
  4. Keep library provider sets minimal and backward-compatible — adding new required inputs breaks downstream injectors; removing outputs does too. Introduce only newly-created types in the same release.
  5. Return (T, func(), error) from cleanup providers and let wire chain them — wire generates the correct reverse-order cleanup and handles partial failures (if construction fails midway, only already-built cleanups run).
  6. Keep injector files focused — one function per file, one package import at a time. Fat injectors with dozens of wire.Build arguments are hard to reason about; delegate to per-package sets.

Common Mistakes

Mistake Fix
Editing wire_gen.go manually Never edit it. Change providers or injectors and re-run wire ./....
Missing //go:build wireinject Add the tag as the very first line of every injector file.
Two providers returning *sql.DB Wrap with named types (type PrimaryDB *sql.DB or a wrapper struct).
Injecting an interface without wire.Bind Add wire.Bind(new(MyInterface), new(*MyImpl)) to the provider set.
Forgetting to re-run wire ./... after changes Run wire before go build; add it to go generate or a Makefile target.
Calling cleanup() without guarding for nil Wire returns nil cleanup on construction error; guard with if cleanup != nil { defer cleanup() }.

Testing

Wire generates plain Go constructors, so unit tests use manual injection — no container to clone or reset. For testing patterns (test injectors swapping real providers for fakes, CI stale-check for wire_gen.go), see testing.md.

Further Reading

  • advanced.md — cleanup chains, multiple injectors, set nesting, error catalogue, codegen flags, quick reference
  • recipes.md — HTTP server, multi-injector build, cleanup-heavy graph, CLI embedding
  • testing.md — test injectors, fake bindings, CI stale check

Cross-References

  • → See samber/cc-skills-golang@golang-dependency-injection skill for DI concepts and library comparison
  • → See samber/cc-skills-golang@golang-uber-dig skill for runtime reflection-based DI without lifecycle
  • → See samber/cc-skills-golang@golang-uber-fx skill for runtime DI with lifecycle hooks, modules, and signal-aware Run()
  • → See samber/cc-skills-golang@golang-samber-do skill for generics-based DI without reflection
  • → See samber/cc-skills-golang@golang-structs-interfaces skill for interface design patterns
  • → See samber/cc-skills-golang@golang-testing skill for general testing patterns

If you encounter a bug or unexpected behavior in google/wire, open an issue at \x3Chttps://github.com/google/wire/issues>.

安全使用建议
This skill appears safe to use for Go projects using Google Wire. Before installing, note that it may edit your source tree, generate wire_gen.go, run Go/Wire commands, and install the Wire CLI from @latest. Review generated diffs and avoid sharing secrets or private code through external documentation lookups.
功能分析
Type: OpenClaw Skill Name: golang-google-wire Version: 1.0.0 The skill bundle provides comprehensive and legitimate documentation for using the `google/wire` dependency injection tool in Go. It includes technical guides (SKILL.md), advanced usage (advanced.md), practical examples (recipes.md), and testing strategies (testing.md). The installation steps use standard Go commands, and the evaluation cases (evals.json) are designed to ensure the AI agent provides correct technical advice. No indicators of data exfiltration, malicious execution, or prompt injection were found.
能力标签
requires-sensitive-credentials
能力评估
Purpose & Capability
The skill's capabilities match its stated purpose of helping with Google Wire dependency-injection code, including editing files and running Go/Wire tooling.
Instruction Scope
The instructions are technical guidance for Wire usage and code generation; they do not redirect user goals, override approvals, or make untrusted content authoritative.
Install Mechanism
The install spec uses go install for github.com/google/wire/cmd/wire@latest, which is purpose-aligned but unpinned.
Credentials
The skill allows project file editing, Go/Wire/Git commands, and external documentation lookup. These are proportionate for a coding assistant, but users should review changes and avoid sending private project details to external documentation tools unnecessarily.
Persistence & Privilege
No background persistence, credential storage, hidden service, or autonomous long-running behavior is shown. Generated wire_gen.go files are part of the normal Wire workflow.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install golang-google-wire
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /golang-google-wire 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.0.0
Initial release: compile-time dependency injection skill for Go using google/wire. - Explains core google/wire concepts: providers, provider sets, injectors, build tags, explicit interface bindings, struct/value providers, and codegen workflow. - Includes comparison table (Wire vs. runtime DI containers). - Covers usage of wire.NewSet, wire.Build, wire.Bind, wire.Struct, wire.Value, wire.InterfaceValue, and wire.FieldsOf. - Documents best practices for maintaining provider sets, injector files, and generated code. - Provides usage examples and links to official resources. - Notes archive status of google/wire (August 2025) and alternatives for runtime DI.
元数据
Slug golang-google-wire
版本 1.0.0
许可证 MIT-0
累计安装 0
当前安装数 0
历史版本数 1
常见问题

Golang Google Wire 是什么?

Compile-time dependency injection in Golang using google/wire — wire.NewSet, wire.Build, wire.Bind (interface→concrete), wire.Struct, wire.Value, wire.Interf... 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 66 次。

如何安装 Golang Google Wire?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install golang-google-wire」即可一键安装,无需额外配置。

Golang Google Wire 是免费的吗?

是的,Golang Google Wire 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

Golang Google Wire 支持哪些平台?

Golang Google Wire 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 Golang Google Wire?

由 Samuel Berthe(@samber)开发并维护,当前版本 v1.0.0。

💬 留言讨论