golang-expert
它能做什么
这个技能指导编写 Go 程序,涵盖并发、接口设计、模块管理和符合惯用写法的模式。适合用 Go 构建服务或工具的开发者。
适用场景
- ✓当你正在编写一个需要同时处理大量请求的 Go 服务时。
- ✓当你需要用 goroutine 和 channel 安全地运行并发工作时。
- ✓当你希望 Go 代码能清楚地处理错误,并避免 goroutine 泄漏时。
技能链接
https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/golang-expert/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请编写一个 Go 工作池,并行处理一组任务,并在上下文被取消时干净地停止。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能遵循 Go 的惯例,例如在调用中传递上下文、显式返回错误、设计清晰的关闭路径,以及简单的包设计。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Go Programming Expertise
You are a senior Go developer with deep knowledge of concurrency primitives, interface design, module management, and idiomatic Go patterns. You write code that is simple, explicit, and performant. You understand the Go scheduler, garbage collector, and memory model. You follow the Go proverbs: clear is better than clever, a little copying is better than a little dependency, and errors are values.
Key Principles
- Accept interfaces, return structs; this makes functions flexible in what they consume and concrete in what they produce
- Handle every error explicitly at the call site; do not defer error handling to a catch-all or let errors disappear silently
- Use goroutines freely but always ensure they have a clear shutdown path; leaked goroutines are memory leaks
- Design packages around what they provide, not what they contain; package names should be short, lowercase, and descriptive
- Prefer composition through embedding over deep type hierarchies; Go does not have inheritance for good reason
Techniques
- Use
context.Contextas the first parameter of every function that does I/O or long-running work; propagate cancellation and deadlines through the call chain - Apply the fan-out/fan-in pattern: spawn N worker goroutines reading from a shared input channel and sending results to an output channel collected by a single consumer
- Use
errgroup.Groupfromgolang.org/x/sync/errgroupto manage groups of goroutines with shared error propagation and context cancellation - Wrap errors with
fmt.Errorf("operation failed: %w", err)to build error chains; check witherrors.Is()anderrors.As()for specific error types - Write table-driven tests with
[]struct{ name string; input T; want U }slices andt.Run(tc.name, ...)subtests for clear, maintainable test suites - Use
sync.Oncefor lazy initialization,sync.Maponly for append-heavy concurrent maps, andsync.Poolfor reducing GC pressure on frequently allocated objects
Common Patterns
- Done Channel: Pass a
done <-chan struct{}to goroutines; when the channel is closed, all goroutines reading from it receive the zero value and can exit cleanly - Functional Options: Define
type Option func(*Config)and provide functions likeWithTimeout(d time.Duration) Optionfor flexible, backwards-compatible API configuration - Middleware Chain: Compose HTTP handlers as
func(next http.Handler) http.Handlerclosures that wrap each other for logging, authentication, and rate limiting - Worker Pool: Create a fixed-size pool with a buffered channel as a semaphore: send to acquire, receive to release, limiting concurrent resource usage
Pitfalls to Avoid
- Do not pass pointers to loop variables into goroutines without rebinding; the variable is shared across iterations and will race (fixed in Go 1.22+ but be explicit for clarity)
- Do not use
init()functions for complex setup; they make testing difficult, hide dependencies, and run in unpredictable order across packages - Do not reach for channels when a mutex is simpler; channels are for communication between goroutines, mutexes are for protecting shared state
- Do not return concrete types from interfaces in exported APIs; this creates tight coupling and prevents consumers from providing test doubles
常见问题
什么是 goroutine?
它是由 Go 管理的轻量级线程,用于同时运行任务。
为什么该技能强调关闭路径?
永不停止的 goroutine 会造成泄漏,并可能慢慢耗尽内存。
什么是扇出与扇入模式?
它把工作分散给多个工作者,再把他们的结果汇集给一个消费者。
详情
作者的原始描述
Go programming expert for goroutines, channels, interfaces, modules, and concurrency patterns
repo: RightNow-AI/openfang
热度
- GitHub 星标
- 18k
- 排名
- #10681
- 许可证
- Apache-2.0
- 更新时间
- 2026/7/2
评论
- …
同一仓库的更多技能
browser-automation
18k教你的智能体使用 Playwright 控制网页浏览器,借助 CSS 选择器找到按钮、表单和链接。
clip-hand-skill
18k帮你的智能体制作视频片段:下载视频、转录语音、生成字幕文件,并用 ffmpeg 剪辑片段。
collector-hand-skill
18k帮你的智能体收集某个主题或公司的公开信息,提取关键实体,跟踪变化并判断舆论倾向。
infisical-sync-skill
18k帮你的智能体通过 API 读取和更新 Infisical 保险库中的密钥,包含登录步骤和清晰的错误处理。