go-error-handling

Standardize Go error handling with %w wrapping and errors.Is checks.

139|17|Updated Jan 27, 2026
One-click install
npx skills add https://github.com/cxuu/golang-skills --skill go-error-handling-cxuu
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-error-handling
Source: https://github.com/cxuu/golang-skills/tree/main/skills/go-error-handling
Command: npx skills add https://github.com/cxuu/golang-skills --skill go-error-handling-cxuu

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Go projects often struggle with inconsistent error handling. This skill consolidates best practices for returning errors, wrapping with context, using sentinel errors, and structuring error types to improve reliability and debugging.

Core Features & Use Cases

  • Standardized error return: always return the error interface and avoid exposing concrete types.
  • Contextual wrapping: use %w to preserve error chains and provide meaningful context.
  • Structured errors: define and check sentinel and structured error types; leverage errors.Is and errors.As.
  • Error flow guidance: guidelines for handling errors once, avoiding log-and-return pitfalls, and composing robust error strategies across packages.

Quick Start

Use this guide to implement consistent Go error handling in your codebase.

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 original error chain?

Wrap errors in Go by using the %w verb in fmt.Errorf to preserve the original error chain. This contextual wrapping adds meaningful information while maintaining the error's underlying cause for reliable debugging and propagation.

When should I use sentinel errors vs dynamic error types in Golang?

Use sentinel errors in Golang for specific known values to check with errors.Is, and dynamic error types for carrying structured data to extract with errors.As. Standardizing both patterns ensures robust error handling strategies across packages.

What is the best way to check specific Go error types in an error flow?

The best way to check specific Go error types in an error flow is using errors.As to extract structured values and errors.Is to match sentinels. These standardize error type checks and propagate reliable error flows.

Why does my Go function return an error but lose the original context?

Your Go function loses original error context when returning concrete types instead of the error interface or skipping %w wrapping. Always return the error interface and wrap with %w to preserve the full error chain for maintainability.

How do I avoid the log-and-return pitfall when handling Go errors?

Avoid the log-and-return pitfall in Go error handling by handling errors exactly once. Apply canonical patterns to either log the error and stop propagating, or wrap and return it without logging to prevent duplicated log entries.