go-error-handling

Implements idiomatic Go error handling with wrapping, inspection, and structured logging.

1|2|Updated Nov 25, 2017
One-click install
npx skills add https://github.com/asarchami/dotfiles --skill go-error-handling-asarchami
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-error-handling
Source: https://github.com/asarchami/dotfiles/tree/main/dot_claude/skills/go-error-handling
Command: npx skills add https://github.com/asarchami/dotfiles --skill go-error-handling-asarchami

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go codebases often suffer from swallowed errors, duplicate log entries, unwrappable error chains, and high-cardinality error messages that break log aggregation dashboards. This Skill enforces idiomatic error handling patterns so errors are always checked, wrapped with context, and logged exactly once. ## Core Features & Use Cases - Error Creation & Wrapping: Guides sentinel errors, custom error types, fmt.Errorf with %w vs %v, and low-cardinality messages for APM tools like Datadog, Loki, and Sentry. - Error Inspection & Handling Rules: Covers errors.Is, errors.As, errors.Join, the single handling rule (log OR return, never both), and panic/recover boundaries. - Structured Logging & Production Errors: Integrates slog and samber/oops for stack traces, user context, and machine-readable error codes. - Use Case: When reviewing a Go PR, activate review mode to detect swallowed errors, log-and-return pairs, and panic misuse; or run audit mode with parallel sub-agents across five error categories. ## Quick Start Ask the AI to review the error handling in your Go service and fix any swallowed errors or missing %w wrapping.

Frequently Asked Questions about go-error-handling

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I wrap errors in Go with context?

Use fmt.Errorf with the %w verb, for example fmt.Errorf("fetching user: %w", err), to preserve the error chain. Use %v only at public API boundaries where you want to hide internal error types from callers.

What is the difference between errors.Is and errors.As in Go?

errors.Is matches an error against a sentinel value anywhere in the wrapped chain, while errors.As extracts a specific typed error from the chain into a target variable. Both traverse wrapped errors, unlike direct comparison or type assertion.

Should I log an error or return it in Go?

Do exactly one: either log the error or return it with added context, never both. Logging and returning creates duplicate entries in log aggregators and makes debugging harder.

When should I use samber/oops instead of the standard errors package?

Use samber/oops for production errors needing stack traces, user or tenant context, error codes, or structured attributes. It is a drop-in replacement compatible with errors.Is and errors.As.

Why should Go error messages avoid variable data like IDs?

Interpolating IDs or paths into error strings creates high-cardinality messages, so each unique value forms a separate group in Datadog, Loki, or Sentry. Attach variable data as structured attributes via slog or oops .With() instead.

When is it acceptable to use panic in Go?

Panic is acceptable only for truly unrecoverable states such as programmer errors or invalid initialization, like MustCompileRegex. Never panic for expected failures like network timeouts, and recover at goroutine or HTTP handler boundaries.