跳到正文
Skillpedia
API 与服务器(后端)18k · #10681

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/golang-expert/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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.Context as 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.Group from golang.org/x/sync/errgroup to 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 with errors.Is() and errors.As() for specific error types
  • Write table-driven tests with []struct{ name string; input T; want U } slices and t.Run(tc.name, ...) subtests for clear, maintainable test suites
  • Use sync.Once for lazy initialization, sync.Map only for append-heavy concurrent maps, and sync.Pool for 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 like WithTimeout(d time.Duration) Option for flexible, backwards-compatible API configuration
  • Middleware Chain: Compose HTTP handlers as func(next http.Handler) http.Handler closures 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能