golang-error-handling

Standardize Go error creation, wrapping, inspection, and structured logging.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

It prevents fragile and noisy error handling in Go by standardizing how errors are created, wrapped, inspected, logged, and exposed across boundaries.

Core Features & Use Cases

  • Idiomatic error chains: Wrap with %w to preserve context, and use errors.Is and errors.As/AsType for matching and typed extraction.
  • Production-safe logging: Apply the single handling rule (log OR return), use slog for structured logging, and keep log messages low-cardinality while attaching variable data as attributes.
  • Robust multi-error handling: Combine independent failures with errors.Join and use sentinel errors and custom error types for expected vs. structured conditions.
  • Safety guidance: Avoid panic for expected failures and use recover only at appropriate goroutine boundaries.

Quick Start

Use the golang-error-handling skill when you are implementing or reviewing Go code that creates, wraps, inspects, or logs errors so that your error chains and structured logs remain consistent and production-ready.

Frequently Asked Questions about golang-error-handling

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

FAQPage Schema
How do I wrap Go errors with context using %w?

To wrap Go errors with context, use the %w verb in fmt.Errorf to preserve the original error chain. This allows downstream callers to unwrap and inspect errors using errors.Is and errors.As while maintaining the full context for production debugging.

What is the best way to handle multiple independent errors in golang?

The best way to handle multiple independent errors in golang is using errors.Join. This function combines multiple independent failures into a single error object, allowing you to aggregate and return them together without losing individual error details.

How does slog work with error handling to reduce log noise?

slog works with error handling by enforcing a single handling rule: either log the error OR return it, but never both. It uses low-cardinality log messages with variable data attached as structured attributes, which significantly reduces production log noise.

When should I use errors.Is versus errors.As in Go?

Use errors.Is to check if an error matches a specific sentinel error value, and use errors.As to extract a typed custom error from the chain. This distinction is critical for inspecting wrapped errors correctly across HTTP and service boundaries.

Does idiomatic Go error handling require lowercase error strings?

Yes, idiomatic Go error handling requires strict lowercase error strings. This standardizes error messages across creation, wrapping, and logging boundaries, ensuring consistency and preventing stylistic noise in production logs.

When should I use recover for panic handling in Go services?

Use recover only at appropriate goroutine boundaries to prevent total service crashes from unexpected panics. You should avoid using panic for expected failures, relying instead on standard error returns for expected conditions.