golang-dependency-injection
samber/cc-skills-golang
Go语言中依赖注入(DI)的综合指南。内容涵盖依赖注入的重要性(可测试性、松耦合、关注点分离、生命周期管理)、手动构造函数注入,以及依赖注入库的对比(google/wire、uber-go/dig、uber-go/fx、samber/do)。 在设计服务架构、配置依赖注入、重构紧密耦合的代码、管理单例或服务工厂时,或者当用户询问控制反转、服务容器等概念时,均可运用此技能。
...展开全部关于golang-dependency-injection
golang-dependency-injection 是一个专注于特定工作流的可复用AI技能。名称:golang-dependency-injection
该技能整合了操作指南、规范以及针对特定任务的指导,以便代理能够更一致地执行任务。描述:“Go语言中依赖注入(DI)的综合指南。 内容涵盖依赖注入的重要性(可测试性、松耦合、关注点分离、生命周期管理)、手动构造函数注入,以及依赖注入库的对比(google/wire、uber-go/dig、uber-go/fx、samber/do)。 在设计服务架构、配置依赖注入、重构紧密耦合的代码、管理单例或服务工厂时,或者当用户询问 Go 语言中的控制反转、服务容器或依赖连接时,请使用此技能。 关于具体的DI库,→ 请参阅 `samber/cc-skills-golang@golang-google-wire`、`samber/cc-skills-golang@golang-uber-dig`、 `samber/cc-skills-golang@golang-uber-fx` 或 `samber/cc-skills-golang@golang-samber-do` 技能。”兼容性:专为 Claude Code 或类似的 AI 编码代理设计,也适用于使用 Golang 的项目。主页: https://github.com/samber/cc-skills-golang
实际上,该技能最适合需要可重复执行、且设置步骤更少、模糊性更低的用户。 支持的工具:读取 编辑 写入 Glob Grep Bash(go:*) Bash(golangci-lint:*) Bash(git:*) 代理 WebFetch mcp__context7__resolve-library-id mcp__context7__query-docs AskUserQuestion **角色设定:** 您是一位 Go 软件架构师。您引导团队实现可测试、松耦合的设计——您会选择能解决问题且最简单的依赖注入方案,并且绝不进行过度设计。 - **设计模式**(新项目、新服务,或向现有 DI 架构添加服务):评估现有的依赖关系图和生命周期需求;根据决策表推荐手动注入或使用相应库;随后生成连接代码。 - **重构模式**(现有耦合代码):最多使用 3 个并行子代理——代理 1 识别全局变量和 `init()` 服务初始化, 代理 2 映射应转换为接口的具体类型依赖关系,代理 3 定位服务定位器反模式(将容器作为参数传递)——随后整合分析结果并提出迁移方案。
常见问题
golang-dependency-injection 能提供哪些帮助?
golang-dependency-injection 帮助代理遵循源文档中描述的聚焦工作流,减少歧义,并确保执行与预期任务保持一致。
何时应使用此技能?
当任务与技能文档中描述的工作流、领域或操作规则相符时,应使用该技能,尤其是在需要保持执行一致性时。
主要限制有哪些?
该技能受其源指令的质量和范围的限制。如果基础文档不完整,客服人员可能仍需要额外的上下文信息或手动验证。
Persona: You are a Go software architect. You guide teams toward testable, loosely coupled designs — you choose the simplest DI approach that solves the problem, and you never over-engineer.
Modes:
- Design mode (new project, new service, or adding a service to an existing DI setup): assess the existing dependency graph and lifecycle needs; recommend manual injection or a library from the decision table; then generate the wiring code.
- Refactor mode (existing coupled code): use up to 3 parallel sub-agents — Agent 1 identifies global variables and
init()service setup, Agent 2 maps concrete type dependencies that should become interfaces, Agent 3 locates service-locator anti-patterns (container passed as argument) — then consolidate findings and propose a migration plan.
Community default. A company skill that explicitly supersedes
samber/cc-skills-golang@golang-dependency-injectionskill takes precedence.
Dependency Injection in Go
Dependency injection (DI) means passing dependencies to a component rather than having it create or find them. In Go, this is how you build testable, loosely coupled applications — your services declare what they need, and the caller (or container) provides it.
This skill is not exhaustive. When using a DI library (google/wire, uber-go/dig, uber-go/fx, samber/do), refer to the library's official documentation and code examples for current API signatures.
For interface-based design foundations (accept interfaces, return structs), see the samber/cc-skills-golang@golang-structs-interfaces skill.
Best Practices Summary
- Dependencies MUST be injected via constructors — NEVER use global variables or
init()for service setup - Small projects (< 10 services) SHOULD use manual constructor injection — no library needed
- Interfaces MUST be defined where consumed, not where implemented — accept interfaces, return structs
- NEVER use global registries or package-level service locators
- The DI container MUST only exist at the composition root (
main()or app startup) — NEVER pass the container as a dependency - Prefer lazy initialization — only create services when first requested
- Use singletons for stateful services (DB connections, caches) and transients for stateless ones
- Mock at the interface boundary — DI makes this trivial
- Keep the dependency graph shallow — deep chains signal design problems
- Choose the right DI library for your project size and team — see the decision table below
Why Dependency Injection?
| Problem without DI | How DI solves it |
|---|---|
| Functions create their own dependencies | Dependencies are injected — swap implementations freely |
| Testing requires real databases, APIs | Pass mock implementations in tests |
| Changing one component breaks others | Loose coupling via interfaces — components don't know each other's internals |
| Services initialized everywhere | Centralized container manages lifecycle (singleton, factory, lazy) |
| All services loaded at startup | Lazy loading — services created only when first requested |
Global state and init() functions | Explicit wiring at startup — predictable, debuggable |
DI shines in applications with many interconnected services — HTTP servers, microservices, CLI tools with plugins. For a small script with 2-3 functions, manual wiring is fine. Don't over-engineer.
Manual Constructor Injection (No Library)
For small projects, pass dependencies through constructors. See Manual DI examples for a complete application example.
// ✓ Good — explicit dependencies, testabletype UserService struct { db UserStore mailer Mailer logger *slog.Logger}func NewUserService(db UserStore, mailer Mailer, logger *slog.Logger) *UserService { return &UserService{db: db, mailer: mailer, logger: logger}}// main.go — manual wiringfunc main() { logger := slog.Default() db := postgres.NewUserStore(connStr) mailer := smtp.NewMailer(smtpAddr) userSvc := NewUserService(db, mailer, logger) orderSvc := NewOrderService(db, logger) api := NewAPI(userSvc, orderSvc, logger) api.ListenAndServe(":8080")}
// ✗ Bad — hardcoded dependencies, untestabletype UserService struct { db *sql.DB}func NewUserService() *UserService { db, _ := sql.Open("postgres", os.Getenv("DATABASE_URL")) // hidden dependency return &UserService{db: db}}
Manual DI breaks down when:
- You have 15+ services with cross-dependencies
- You need lifecycle management (health checks, graceful shutdown)
- You want lazy initialization or scoped containers
- Wiring order becomes fragile and hard to maintain
DI Library Comparison
Go has three main approaches to DI libraries:
- google/wire examples — Compile-time code generation
- uber-go/dig + fx examples — Reflection-based framework
- samber/do examples — Generics-based, no code generation
Decision Table
| Criteria | Manual | google/wire | uber-go/dig + fx | samber/do |
|---|---|---|---|---|
| Project size | Small (< 10 services) | Medium-Large | Large | Any size |
| Type safety | Compile-time | Compile-time (codegen) | Runtime (reflection) | Compile-time (generics) |
| Code generation | None | Required (wire_gen.go) | None | None |
| Reflection | None | None | Yes | None |
| API style | N/A | Provider sets + build tags | Struct tags + decorators | Simple, generic functions |
| Lazy loading | Manual | N/A (all eager) | Built-in (fx) | Built-in |
| Singletons | Manual | Built-in | Built-in | Built-in |
| Transient/factory | Manual | Manual | Built-in | Built-in |
| Scopes/modules | Manual | Provider sets | Module system (fx) | Built-in (hierarchical) |
| Health checks | Manual | Manual | Manual | Built-in interface |
| Graceful shutdown | Manual | Manual | Built-in (fx) | Built-in interface |
| Container cloning | N/A | N/A | N/A | Built-in |
| Debugging | Print statements | Compile errors | fx.Visualize() | ExplainInjector(), web interface |
| Go version | Any | Any | Any | 1.18+ (generics) |
| Learning curve | None | Medium | High | Low |
Quick Comparison: Same App, Four Ways
The dependency graph: Config -> Database -> UserStore -> UserService -> API
Manual:
cfg := NewConfig()db := NewDatabase(cfg)store := NewUserStore(db)svc := NewUserService(store)api := NewAPI(svc)api.Run()// No automatic shutdown, health checks, or lazy loading
google/wire:
// wire.go — then run: wire ./...func InitializeAPI() (*API, error) { wire.Build(NewConfig, NewDatabase, NewUserStore, NewUserService, NewAPI) return nil, nil}// No lifecycle hooks (OnStart/OnStop) or health checks; cleanup via returned func() from providers
uber-go/fx:
app := fx.New( fx.Provide(NewConfig, NewDatabase, NewUserStore, NewUserService), fx.Invoke(func(api *API) { api.Run() }),)app.Run() // manages lifecycle, but reflection-based
samber/do:
i := do.New()do.Provide(i, NewConfig)do.Provide(i, NewDatabase) // auto shutdown + health checkdo.Provide(i, NewUserStore)do.Provide(i, NewUserService)api := do.MustInvoke[*API](i)api.Run()// defer i.Shutdown() — handles all cleanup automatically
Testing with DI
DI makes testing straightforward — inject mocks instead of real implementations:
// Define a mocktype MockUserStore struct { users map[string]*User}func (m *MockUserStore) FindByID(ctx context.Context, id string) (*User, error) { u, ok := m.users[id] if !ok { return nil, ErrNotFound } return u, nil}// Test with manual injectionfunc TestUserService_GetUser(t *testing.T) { mock := &MockUserStore{ users: map[string]*User{"1": {ID: "1", Name: "Alice"}}, } svc := NewUserService(mock, nil, slog.Default()) user, err := svc.GetUser(context.Background(), "1") if err != nil { t.Fatalf("unexpected error: %v", err) } if user.Name != "Alice" { t.Errorf("got %q, want %q", user.Name, "Alice") }}
Testing with samber/do — Clone and Override
Container cloning creates an isolated copy where you override only the services you need to mock:
func TestUserService_WithDo(t *testing.T) { // Create a test injector with mock implementation testInjector := do.New() // Provide the mock UserStore interface do.OverrideValue[UserStore](testInjector, &MockUserStore{ users: map[string]*User{"1": {ID: "1", Name: "Alice"}}, }) // Provide other real services as needed do.Provide[*slog.Logger](testInjector, func(i *do.Injector) (*slog.Logger, error) { return slog.Default(), nil }) svc := do.MustInvoke[*UserService](testInjector) user, err := svc.GetUser(context.Background(), "1") // ... assertions}
This is particularly useful for integration tests where you want most services to be real but need to mock a specific boundary (database, external API, mailer).
When to Adopt a DI Library
| Signal | Action |
|---|---|
| < 10 services, simple dependencies | Stay with manual constructor injection |
| 10-20 services, some cross-cutting concerns | Consider a DI library |
| 20+ services, lifecycle management needed | Strongly recommended |
| Need health checks, graceful shutdown | Use a library with built-in lifecycle support |
| Team unfamiliar with DI concepts | Start manual, migrate incrementally |
Common Mistakes
| Mistake | Fix |
|---|---|
| Global variables as dependencies | Pass through constructors or DI container |
init() for service setup | Explicit initialization in main() or container |
| Depending on concrete types | Accept interfaces at consumption boundaries |
| Passing the container everywhere (service locator) | Inject specific dependencies, not the container |
| Deep dependency chains (A->B->C->D->E) | Flatten — most services should depend on repositories and config directly |
| Creating a new container per request | One container per application; use scopes for request-level isolation |
Cross-References
- → See
samber/cc-skills-golang@golang-samber-doskill for detailed samber/do usage patterns - → See
samber/cc-skills-golang@golang-structs-interfacesskill for interface design and composition - → See
samber/cc-skills-golang@golang-testingskill for testing with dependency injection - → See
samber/cc-skills-golang@golang-project-layoutskill for DI initialization placement
References
- samber/do/v2 documentation | github.com/samber/do/v2
- google/wire user guide
- uber-go/fx documentation
- uber-go/dig
所有文件
6 个文件安装 golang-dependency-injection
下载技能文件并将其解压到 .claude/skills/ 目录中。
下载ZIP克隆仓库并复制技能文件到您的项目中。
git clone https://github.com/samber/cc-skills-golang/blob/main/skills/golang-dependency-injection/SKILL.md # Copy SKILL.md to your .claude/skills/ directory
复制





首页
