模式
- 作者仓库星标 0
- 作者仓库 skills-registry
- 领域
- 通用
- 作者声明的智能体
- 未找到明确声明;不据此推断已兼容或已测试
- 静态检查
- 88 / 100 · 启发式扫描,不代表运行安全
- 运行环境证据
- 未执行;当前没有隔离运行测试证据
- 作者 / 版本 / 许可证
- @tomevault-io · 未声明许可证
- 上下文用量评级
- 低消耗
- 接入复杂程度
- 需简单配置
- 是否需要外部接口密钥
- 不需要
- 兼容的系统
- 未声明(默认跨平台)
- 运行环境要求
- 未检出明确要求
- 检测到的文件与系统行为
-
- 只读
- 允许写入 / 修改
- 命令执行
- 读取环境变量
- 网络行为
- 仅限本地
- 安装命令数
- 无(仅作为资料)
需要注意: 未声明工具权限范围;实际权限由运行环境决定。
// worker runs until ctx is cancelled or an error occurs.
// Errors are returned via errCh; the caller must drain it.
func worker(ctx context.Context, jobs <-chan Job, errCh chan<- error) {
for {
select {
case <-ctx.Done():
errCh <- fmt.Errorf("worker cancelled: %w", ctx.Err())
return
case job, ok := <-jobs:
if !ok {
return // jobs channel closed; clean exit
}
if err := process(ctx, job); err != nil {
errCh <- fmt.Errorf("process job %v: %w", job.ID, err)
return
}
}
}
}
func runPipeline(ctx context.Context, jobs []Job) error {
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
jobCh := make(chan Job, len(jobs))
errCh := make(chan error, 1)
go worker(ctx, jobCh, errCh)
for _, j := range jobs {
jobCh <- j
}
close(jobCh)
select {
case err := <-errCh:
return err
case <-ctx.Done():
return fmt.Errorf("pipeline timed out: %w", ctx.Err())
}
}
# Golang Patterns
Idiomatic Go reference pack — concurrency, interfaces, generics, testing,
project structure, plus the anti-patterns agents most commonly get wrong.
Two-thesis stack:
- **Clear is better than clever.** Boring, explicit, maintainable Go (Jon Bodner,
*Learning Go*, 2nd ed.).
- **Production-grade by default.** Bounded goroutine lifetimes, context
threading, race-detector-clean tests, gofmt + golangci-lint on every change.
## Reference Guide
Load detailed guidance based on context:
| Topic | Reference | Load When |
|-------|-----------|-----------|
| Concurrency | `references/concurrency.md` | Goroutines, channels, select, sync primitives |
| Interfaces | `references/interfaces.md` | Interface design, io.Reader/Writer, composition |
| Generics | `references/generics.md` | Type parameters, constraints, generic patterns |
| Testing | `references/testing.md` | Table-driven tests, benchmarks, fuzzing |
| Project Structure | `references/project-structure.md` | Module layout, internal packages, go.mod |
| Idiomatic Go (anti-patterns) | `references/idiomatic-go.md` | Quick anti-pattern → idiomatic-fix tables, decision rules, agent-specific rationalization counters |
## Core Workflow
1. **Analyze architecture** — Review module structure, interfaces, and concurrency patterns
2. **Design interfaces** — Small, focused, defined at the consumer; composition over inheritance
3. **Implement** — Idiomatic Go with proper error handling and context propagation; run `go vet ./...` before proceeding
4. **Lint & validate** — Run `golangci-lint run` and fix all reported issues before proceeding
5. **Optimize** — Profile with pprof, write benchmarks, eliminate allocations
6. **Test** — Table-driven tests with `-race`, fuzzing, 80%+ coverage; race detector must pass before committing
## Core Pattern Example
Goroutine with context cancellation and error propagation:
```go
// worker runs until ctx is cancelled or an error occurs.
// Errors are returned via errCh; the caller must drain it.
func worker(ctx context.Context, jobs <-chan Job, errCh chan<- error) {
for {
select {
case <-ctx.Done():
errCh <- fmt.Errorf("worker cancelled: %w", ctx.Err())
return
case job, ok := <-jobs:
if !ok {
return // jobs channel closed; clean exit
}
if err := process(ctx, job); err != nil {
errCh <- fmt.Errorf("process job %v: %w", job.ID, err)
return
}
}
}
}
func runPipeline(ctx context.Context, jobs []Job) error {
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
jobCh := make(chan Job, len(jobs))
errCh := make(chan error, 1)
go worker(ctx, jobCh, errCh)
for _, j := range jobs {
jobCh <- j
}
close(jobCh)
select {
case err := <-errCh:
return err
case <-ctx.Done():
return fmt.Errorf("pipeline timed out: %w", ctx.Err())
}
}
```
Key properties: bounded goroutine lifetime via `ctx`, error propagation with
`%w`, no goroutine leak on cancellation.
## Constraints
### MUST DO
- Run gofmt and golangci-lint on all code
- Add `context.Context` as first param on all blocking operations
- Handle every error explicitly (no naked `_` discards without justification)
- Write table-driven tests with subtests
- Document all exported functions, types, and packages
- Use `X | Y` union constraints for generics (Go 1.18+)
- Propagate errors with `fmt.Errorf("...: %w", err)`
- Run race detector on tests (`-race` flag)
### MUST NOT DO
- Ignore errors (no `_ = thatMightFail()` without a comment)
- Use `panic` for normal error handling
- Spawn goroutines without a clear lifecycle
- Skip context cancellation handling
- Reach for reflection without a measured performance reason
- Mix sync and async patterns carelessly
- Hardcode configuration (functional options or env vars)
## Output Templates
When implementing Go features, provide:
1. Interface definitions (contracts first)
2. Implementation files with proper package structure
3. Test file with table-driven tests
4. Brief explanation of any concurrency patterns used
## Pairing
- **`golang-pro` agent** — broader architectural / DevOps coverage; delegates
pattern detail here. Load both when active Go development is in scope.
## Provenance
Initial content adapted from
[jeffallan/claude-skills](https://github.com/jeffallan/claude-skills) (MIT,
`skills/golang-pro`) and the prior wardrobe `idiomatic-go` skill (Bodner-derived
anti-pattern tables, now at `references/idiomatic-go.md`). See [`LICENSES.md`](../../LICENSES.md).
---
> Source: [danmestas/wardrobe](https://github.com/danmestas/wardrobe) — distributed by [TomeVault](https://tomevault.io).
<!-- tomevault:4.0:skill_md:2026-05-23 -->
章节与文件索引
按说明文件中的顺序排列。
参考指南
核心工作流程
核心模式示例
必须做
绝对不能做
文件/命令 -> references/concurrency.md · references/interfaces.md · references/generics.md · references/testing.md · references/project-structure.md · references/idiomatic-go.md · go vet ./... · golangci-lint run
内容校验值 -> c1fc7daca612
当前原文的自动摘译,尚未人工审校。
参考指南
根据上下文加载详细指导:主题·参考·并发时加载·
references/concurrency.md·Goroutines、通道、选择、同步原语接口·references/interfaces.md·接口设计、io.Reader/Writer、组合泛型·references/generics.md·类型参数、约束、泛型模式核心工作流程
分析架构——审查模块结构、接口和并发模式 设计接口——小型、集中、在消费者处定义;组合优于继承 实现 — 具有适当错误处理和上下文传播的惯用 Go;在继续 Lint 和验证之前运行
go vet ./...— 在继续之前运行golangci-lint run并修复所有报告的问题 优化 — 使用 pprof 进行分析,编写基准测试,消除分配核心模式示例
具有上下文取消和错误传播的 Goroutine:关键属性:通过
ctx有界 Goroutine 生命周期,通过%w进行错误传播,取消时没有 Goroutine 泄漏。必须做
在所有代码上运行 gofmt 和 golangci-lint 将
context.Context添加为所有阻塞操作的第一个参数 显式处理每个错误(没有裸露的_在没有理由的情况下丢弃) 使用子测试编写表驱动测试 记录所有导出的函数、类型和包绝对不能做
忽略错误(没有
_ = thatMightFail()没有注释) 使用panic进行正常错误处理 生成没有明确生命周期的 goroutine 跳过上下文取消处理 在没有测量性能原因的情况下进行反射