go-error-handling

Implement idiomatic Go error wrapping with %w and errors.Is/As.

64|9|Updated Mar 27, 2026
One-click install
npx skills add https://github.com/eduardo-sl/go-agent-skills --skill go-error-handling-eduardo-sl
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-error-handling
Source: https://github.com/eduardo-sl/go-agent-skills/tree/main/skills/%28code-quality%29/go-error-handling
Command: npx skills add https://github.com/eduardo-sl/go-agent-skills --skill go-error-handling-eduardo-sl

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Go's explicit error values require consistent patterns to make errors actionable, debuggable, and safe to handle across package boundaries. This skill codifies idiomatic practices for creating, wrapping, and inspecting errors so callers can reliably detect conditions and operators can diagnose issues.

Core Features & Use Cases

  • Error decision flow: when to use simple errors, wrapping with context, sentinel vars, or custom types.
  • Wrapping and chain preservation: guidance to always add context with %w when propagating errors unless intentionally hiding implementation details.
  • Sentinel and typed errors: patterns for detectable sentinel vars and custom error types consumed via errors.Is and errors.As.
  • Review and verification checklist: common anti-patterns to avoid such as log-and-return, == comparisons, and broken error chains.
  • Use Case: review a PR or implement a library function to ensure errors are wrapped, named, and typed so higher layers can make control decisions and observability remains useful.

Quick Start

Use the go-error-handling skill to review a Go function and rewrite its error returns to add contextual wrapping with %w, replace direct comparisons with errors.Is/errors.As, and introduce sentinel or custom error types where callers need to detect specific conditions.

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 to preserve the error chain?

To wrap errors in Go and preserve the chain, use fmt.Errorf with the %w verb when propagating errors upward. This adds context while keeping the original error accessible via errors.Is and errors.As for reliable condition detection.

When should I use sentinel errors vs custom error types in Go?

Use sentinel errors in Go for specific package-level conditions that callers check with errors.Is, and use custom error types when callers need to extract structured data via errors.As. This distinction ensures errors remain both detectable and informative across boundaries.

What is the best way to handle multiple errors in Go?

The best way to handle multiple errors in Go is using errors.Join to aggregate them. This function combines multiple errors into a single error value, preserving the individual error chains so operators can diagnose all concurrent failures effectively.

Why does log-and-return cause problems in Go error handling?

Log-and-return causes problems in Go error handling because it breaks the error chain and duplicates error inspection. When you log an error before returning it, higher layers lose the raw error value, making it difficult to use errors.Is or errors.As for control flow decisions.

How do I inspect wrapped errors in Go using errors.As?

To inspect wrapped errors in Go using errors.As, pass a pointer to the target error type you expect to find. It traverses the error chain and assigns the first matching error, allowing you to extract specific fields from custom error types safely.