Golang Error Handling
Samuel BertheCommunityCreated by samberDistributed by Samuel Berthe
- Skill
- Guidelines
- Software development
- MIT
- No issues found
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a structured approach to Go error management, emphasizing maintainability and observability. It covers the full lifecycle of an error, from creation and wrapping to inspection and logging. By adhering to the single handling rule and leveraging modern Go features like slog and errors.Join, developers can create robust systems that provide clear diagnostic information without cluttering logs with duplicate entries.
What it does
- Defines best practices for error creation, wrapping, and inspection.
- Enforces the single handling rule to prevent duplicate log noise.
- Provides patterns for structured logging using slog.
- Guides the appropriate use of panic and recover for unrecoverable states.
- Supports auditing codebases for common error handling anti-patterns.
- Integrates with samber/oops for enhanced production error context.
When to use it
- When writing new Go code that requires robust error propagation.
- During code reviews to identify swallowed or improperly handled errors.
- When auditing existing codebases for reliability and observability improvements.
- When setting up production logging and error monitoring pipelines.
Advantages
- Reduces log noise by enforcing the single handling rule.
- Improves debuggability with structured context and stack traces.
- Ensures consistent error handling patterns across large teams.
- Facilitates better integration with modern log aggregation tools.
Usage examples
- “Review my Go function for idiomatic error handling.”
- The agent will analyze the code for swallowed errors, missing context wrapping, and ensure the single handling rule is followed, suggesting improvements for better observability.
- “Audit my codebase for panic/recover usage.”
- The agent will scan the project for improper panic usage and verify that recovery mechanisms are correctly implemented at goroutine boundaries.
- “How should I wrap errors in my service layer?”
- The agent will provide examples using fmt.Errorf with %w to maintain error chains, ensuring context is added without losing the original error type.
Install
Download the skill as a ZIP:
https://autogineer.com/agent-skills/samber/golang-error-handling/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip golang-error-handling.zip -d .agents/skills
Documentation by samber
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.
Orchestration mode: Fan out the five category sub-agents described in the "Parallelizing Error Handling Audits" section (creation, wrapping, single-handling rule, panic/recover, structured logging) for auditing error handling across a large codebase, and consolidate their findings. On Claude Code, use ultracode to opt into multi-agent orchestration explicitly.
Modes:
Coding mode — writing new error handling code. Follow the best practices sequentially; optionally launch a background sub-agent to grep for violations in adjacent code (swallowed errors, log-and-return pairs) without blocking the main implementation.
Review mode — reviewing a PR's error handling changes. Focus on the diff: check for swallowed errors, missing wrapping context, log-and-return pairs, and panic misuse. Sequential.
Audit mode — auditing existing error handling across a codebase. Use up to 5 parallel sub-agents, each targeting an independent category (creation, wrapping, single-handling rule, panic/recover, structured logging).
Community default. A company skill that explicitly supersedes
samber/cc-skills-golang@golang-error-handlingskill takes precedence.
Go Error Handling Best Practices
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.
Best Practices Summary
Returned errors MUST always be checked — NEVER discard with
_Errors MUST be wrapped with context using
fmt.Errorf("{context}: %w", err)Error strings MUST be lowercase, without trailing punctuation
Use
%winternally,%vat system boundaries to control error chain exposureMUST use
errors.Isfor sentinel matching anderrors.As/errors.AsTypefor typed chain inspection instead of direct comparison or bare type assertions. For Go 1.26+, prefererrors.AsType[T](err)whenTimplementserror; useerrors.As(err, &target)for Go <1.26 or for non-error interface targets.SHOULD use
errors.Join(Go 1.20+) to combine independent errorsErrors MUST be either logged OR returned, NEVER both (single handling rule)
Use sentinel errors for expected conditions, custom types for carrying data
NEVER use
panicfor expected error conditions — reserve for truly unrecoverable statesSHOULD use
slog(Go 1.21+) for structured error logging — notfmt.Printlnorlog.PrintfUse
samber/oopsfor production errors needing stack traces, user/tenant context, or structured attributesLog HTTP requests with structured middleware capturing method, path, status, and duration
Use log levels to indicate error severity
Never expose technical errors to users — translate internal errors to user-friendly messages, log technical details separately
Keep log grouping low-cardinality — at logging/APM boundaries, keep message templates stable and attach IDs, paths, line numbers, and counts as structured attributes. Error values may include useful operational context, but avoid putting high-cardinality data into the stable log message used for grouping.
Detailed Reference
Error 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)beatsfmt.Errorf("{context}: %v", err)(chains vs concatenation). How to inspect chains witherrors.Is,errors.As, and Go 1.26+errors.AsTypefor type-safe error handling, anderrors.Joinfor 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,
samber/oopsfor production errors, andslogstructured logging integration for APM tools.
Parallelizing Error Handling Audits
When auditing error handling across a large codebase, use up to 5 parallel sub-agents — each targets an independent error category:
Sub-agent 1: Error creation — validate
errors.New/fmt.Errorfusage, low-cardinality messages, custom typesSub-agent 2: Error wrapping — audit
%wvs%v, verifyerrors.Is/errors.AspatternsSub-agent 3: Single handling rule — find log-and-return violations, swallowed errors, discarded errors (
_)Sub-agent 4: Panic/recover — audit
panicusage, verify recovery at goroutine boundariesSub-agent 5: Structured logging — verify
slogusage at error sites, check for PII in error messages
Cross-References
→ See
samber/cc-skills-golang@golang-samber-oopsfor full samber/oops API, builder patterns, and logger integration→ See
samber/cc-skills-golang@golang-observabilityfor structured logging setup, log levels, and request logging middleware→ See
samber/cc-skills-golang@golang-safetyfor nil interface trap and nil error comparison pitfalls→ See
samber/cc-skills-golang@golang-namingfor error naming conventions (ErrNotFound, PathError)→ See
samber/cc-skills-golang@golang-continuous-integrationskill for automated AI-driven code review in CI using these guidelines
References
Files
- ATTRIBUTION.mdAdded by AutoGineer920 B
- LICENSELicence file added by AutoGineer1.0 KiB
- SKILL.md7.2 KiB
- evals/evals.json15.4 KiB
- references/error-creation.md5.6 KiB
- references/error-handling.md4.5 KiB
- references/error-wrapping.md3.8 KiB
ATTRIBUTION.md
Attribution
This folder is an unmodified copy of the Agent Skill "golang-error-handling", distributed by AutoGineer (https://autogineer.com/agent-skills).
Commit: bb1d43d98a3f8ab639a0042f90d0ec524e39a477
Created by: samber (https://github.com/samber/cc-skills-golang/tree/bb1d43d98a3f8ab639a0042f90d0ec524e39a477/skills/golang-error-handling)
Publisher: Samuel Berthe (https://github.com/samber)
Licence: MIT License (https://spdx.org/licenses/MIT.html)
Licence evidence: declared in the frontmatter ("license: MIT")
Retrieved: 2026-11-07
The skill's own files are unmodified copies. AutoGineer is not affiliated with or endorsed by the author or the publisher.
Files added by AutoGineer
ATTRIBUTION.md: this file, written by AutoGineer
LICENSE: copied from LICENSE
Requirements and compatibility
Written for: Claude Code, OpenAI Codex
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Needs: Declares allowed tools, Claude Code frontmatter
The author says: Designed for Claude Code, Codex or similar harness, and for projects using Golang.
Security
No issues found by our static scan· No issues found
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- samber
- Publisher
- Samuel Berthe (Community)
- Distributed by
- Samuel Berthe
- Source
- samber/cc-skills-golang · skills/golang-error-handling · commit bb1d43d
- Licence
- MIT License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by samber or Samuel Berthe.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download golang-error-handling.zip