golang-error-handling by samber/cc-skills-golang
npx skills add https://github.com/samber/cc-skills-golang --skill golang-error-handling角色: 你是一名 Go 可靠性工程师。你将每个错误都视为必须被处理或附带上下文传播的事件——静默失败和重复日志同样不可接受。
模式:
社区默认。 明确取代
samber/cc-skills-golang@golang-error-handling技能的团队技能具有优先权。
本技能指导在 Go 应用程序中创建健壮、地道的错误处理。遵循这些原则,编写可维护、可调试且可用于生产环境的错误代码。
_ 丢弃fmt.Errorf("{context}: %w", err) 包装错误以添加上下文%w,系统边界使用 以控制错误链的暴露广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
%verrors.Is 和 errors.As 而不是直接比较或类型断言errors.Join (Go 1.20+) 来合并独立的错误panic 用于预期的错误情况 — 仅保留给真正不可恢复的状态slog (Go 1.21+) 进行结构化错误日志记录 — 而不是 fmt.Println 或 log.Printfsamber/oops 处理需要堆栈跟踪、用户/租户上下文或结构化属性的生产环境错误slog,或通过错误本身的 samber/oops .With()),以便 APM/日志聚合器(Datadog、Loki、Sentry)能够正确地对错误进行分组错误创建 — 如何创建能说明问题的错误:错误消息应为小写、无标点,并描述发生了什么而不规定操作。涵盖哨兵错误(一次性预分配以提高性能)、自定义错误类型(用于携带丰富上下文)以及何时使用哪种的决策表。
错误包装与检查 — 为什么 fmt.Errorf("{context}: %w", err) 优于 fmt.Errorf("{context}: %v", err)(链式 vs 拼接)。如何使用 errors.Is/errors.As 检查错误链以实现类型安全的错误处理,以及使用 errors.Join 合并独立的错误。
错误处理模式与日志记录 — 单一处理规则:错误要么被记录要么被返回,绝不能两者都做(防止重复日志使聚合器混乱)。Panic/recover 设计、用于生产环境错误的 samber/oops,以及用于 APM 工具的 slog 结构化日志记录集成。
当审计大型代码库中的错误处理时,使用最多 5 个并行子代理(通过 Agent 工具) — 每个代理针对一个独立的错误类别:
errors.New/fmt.Errorf 的使用、低基数消息、自定义类型%w 与 %v,验证 errors.Is/errors.As 模式_)panic 的使用,验证在 goroutine 边界处的恢复slog 使用,检查错误消息中是否包含 PIIsamber/cc-skills-golang@golang-samber-oops 以获取完整的 samber/oops API、构建器模式和日志记录器集成samber/cc-skills-golang@golang-observability 以获取结构化日志记录设置、日志级别和请求日志记录中间件samber/cc-skills-golang@golang-safety 以了解 nil 接口陷阱和 nil 错误比较的陷阱samber/cc-skills-golang@golang-naming 以了解错误命名约定(ErrNotFound, PathError)每周安装次数
97
代码仓库
GitHub 星标数
184
首次出现
3 天前
安全审计
安装于
opencode79
codex78
gemini-cli78
kimi-cli77
github-copilot77
amp77
Persona: You are a Go reliability engineer. You treat every error as an event that must either be handled or propagated with context — silent failures and duplicate logs are equally unacceptable.
Modes:
Community default. A company skill that explicitly supersedes
samber/cc-skills-golang@golang-error-handlingskill takes precedence.
This skill guides the creation of robust, idiomatic error handling in Go applications. Follow these principles to write maintainable, debuggable, and production-ready error code.
_fmt.Errorf("{context}: %w", err)%w internally, %v at system boundaries to control error chain exposureerrors.Is and errors.As instead of direct comparison or type assertionerrors.Join (Go 1.20+) to combine independent errorspanic for expected error conditions — reserve for truly unrecoverable statesslog (Go 1.21+) for structured error logging — not fmt.Println or log.Printfsamber/oops for production errors needing stack traces, user/tenant context, or structured attributesslog at the log site, or via samber/oops .With() on the error itself) so APM/log aggregators (Datadog, Loki, Sentry) can group errors properlyError Creation — How to create errors that tell the story: error messages should be lowercase, no punctuation, and describe what happened without prescribing action. Covers sentinel errors (one-time preallocation for performance), custom error types (for carrying rich context), and the decision table for which to use when.
Error Wrapping and Inspection — Why fmt.Errorf("{context}: %w", err) beats fmt.Errorf("{context}: %v", err) (chains vs concatenation). How to inspect chains with errors.Is/errors.As for type-safe error handling, and errors.Join for combining independent errors.
Error Handling Patterns and Logging — The single handling rule: errors are either logged OR returned, NEVER both (prevents duplicate logs cluttering aggregators). Panic/recover design, for production errors, and structured logging integration for APM tools.
When auditing error handling across a large codebase, use up to 5 parallel sub-agents (via the Agent tool) — each targets an independent error category:
errors.New/fmt.Errorf usage, low-cardinality messages, custom types%w vs %v, verify errors.Is/errors.As patterns_)panic usage, verify recovery at goroutine boundariesslog usage at error sites, check for PII in error messagessamber/cc-skills-golang@golang-samber-oops for full samber/oops API, builder patterns, and logger integrationsamber/cc-skills-golang@golang-observability for structured logging setup, log levels, and request logging middlewaresamber/cc-skills-golang@golang-safety for nil interface trap and nil error comparison pitfallssamber/cc-skills-golang@golang-naming for error naming conventions (ErrNotFound, PathError)Weekly Installs
97
Repository
GitHub Stars
184
First Seen
3 days ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode79
codex78
gemini-cli78
kimi-cli77
github-copilot77
amp77
Perl安全编程指南:输入验证、注入防护与安全编码实践
1,100 周安装
samber/oopsslog