go-error-handling

Standardize Go error handling with %w wrapping and consistent return patterns.

Updated Apr 16, 2026
One-click install
npx skills add https://github.com/HadiCherkaoui/opencode-config --skill go-error-handling-hadicherkaoui
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-error-handling
Source: https://github.com/HadiCherkaoui/opencode-config/tree/main/skills/golang/go-error-handling
Command: npx skills add https://github.com/HadiCherkaoui/opencode-config --skill go-error-handling-hadicherkaoui

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Go error handling is often verbose and inconsistent. This Skill provides a structured, best-practice approach to returning, wrapping, and inspecting errors in Go.

Core Features & Use Cases

  • Returning errors with the correct type and position as the last return value.
  • Wrapping errors with context using %w to preserve the error chain.
  • Designing error types (sentinel vs custom) and using errors.Is/errors.As for robust checking.
  • Use-case: implement a function that fetches a resource and wraps underlying errors for clear logs.

Quick Start

Create a small Go function that returns an error and demonstrates wrapping with %w.

Frequently Asked Questions about go-error-handling

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

FAQPage Schema
What's the best way to wrap Go errors to preserve the error chain?

The best way to wrap Go errors is using the %w verb with fmt.Errorf to preserve the error chain. This standardizes error handling patterns and maintains context for clear logs while allowing robust inspection with errors.Is and errors.As.

How do I choose between sentinel errors and custom error types in Go?

Choosing between sentinel errors and custom error types in Go depends on your API needs. Sentinel errors provide simple public API constants for specific expected conditions, while custom structured error types offer richer context and enable type-safe inspection via errors.As for robust checking.

Why should the error type be the last return value in a Go function?

The error type should be the last return value in a Go function to enforce the error type as a public API and prohibit in-band errors. This standardizes error handling patterns across a project to ensure consistent returns and maintainable error semantics.

How does errors.Is and errors.As work for inspecting wrapped errors?

Inspecting wrapped errors with errors.Is and errors.As works by traversing the error chain created by %w wrapping. errors.Is checks for specific sentinel values, while errors.As extracts custom structured error types, enabling robust checking and clear logs without losing underlying context.

When do I need to standardize Go error handling patterns across a project?

You need to standardize Go error handling patterns across a project when functions create, return, wrap, or inspect errors inconsistently. This ensures consistent error semantics, maintains error chains, and enforces the error type as a public API to prevent in-band errors.

Does this approach to Go error handling prohibit in-band errors?

Yes, this approach to Go error handling prohibits in-band errors by enforcing the error type as a public API and ensuring proper wrapping with %w. This guarantees consistent returns and maintainable error chains across the codebase.