golang-error-handling

Create idiomatic Go error handling patterns with wrapping and structured logging.

2.9k|191|Updated Mar 21, 2026
One-click install
npx skills add https://github.com/samber/cc-skills-golang --skill golang-error-handling-samber
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-error-handling
Source: https://github.com/samber/cc-skills-golang/tree/main/skills/golang-error-handling
Command: npx skills add https://github.com/samber/cc-skills-golang --skill golang-error-handling-samber

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Go projects suffer from error propagation, wrapping, and logging across layers. This Skill provides idiomatic patterns, best practices, and concrete examples to make errors actionable and logs informative at scale.

Core Features & Use Cases

  • Error creation: sentinel errors, custom error types, and well-scoped messages
  • Error wrapping and inspection: using %w, errors.Is, and errors.As
  • Single handling rule: log OR return, never both; middleware and HTTP handlers patterns
  • Panic/recover guidelines for truly unrecoverable states and safe recovery
  • Structured logging with slog and samber/oops for production-grade errors

Quick Start

Provide a compact Go example implementing idiomatic error handling with wrapping, errors.Is/As, and structured logging.

Frequently Asked Questions about golang-error-handling

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

FAQPage Schema
How do I handle Go error wrapping and logging across application layers?

Idiomatic Go error handling uses %w for wrapping and errors.Is/As for inspection, applying a single rule to log or return, never both. This prevents duplicate logs and preserves error context across layers.

What is the best way to use errors.Is and errors.As in Go middleware?

Use errors.Is to check for specific sentinel errors and errors.As to extract custom error types. In Go middleware, inspect wrapped errors to determine HTTP status codes while adhering to the log-or-return rule.

How does structured logging with slog work with Go error handling?

Structured logging with slog attaches contextual fields to errors at production boundaries. Using samber/oops, you log the error once with metadata at the top level, maintaining a clean log-or-return propagation flow.

When should I use panic and recover in Go instead of returning errors?

Use Go panic and recover only for truly unrecoverable states, not for standard control flow. Safe recovery middleware catches panics to prevent application crashes while normal logic relies on error wrapping and returning.

Can I use errors.Join to aggregate multiple Go errors in production apps?

Yes, errors.Join combines multiple errors into a single error chain. This Go feature is suitable for production applications where concurrent operations or validation steps generate multiple distinct failures.