golang-error-handling

Enforce idiomatic Go error handling with wrapping, inspection, and logging.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

It prevents brittle or noisy Go error handling by ensuring every error is created, wrapped, inspected, and logged in an idiomatic, low-duplication way that preserves debuggability.

Core Features & Use Cases

  • Idiomatic error creation and wrapping: enforce lowercase error strings, wrap with fmt.Errorf "{context}: %w", and keep chain integrity for internal layers.
  • Correct inspection and combination: use errors.Is for sentinel checks, errors.As for typed chain extraction, and errors.Join to combine independent failures.
  • Production-ready logging and boundaries: apply the single handling rule (log OR return), use structured logging with slog, and use samber/oops for production errors with stack traces and context.
  • Use Case: while building NetDisk-style services (file uploads, chunking, media processing), apply consistent error patterns so operators can trace failures without duplicate logs or leaking internal details at API boundaries.

Quick Start

Use the skill while implementing a Go function that returns errors (and ensure all errors are checked, wrapped with %w, inspected with errors.Is/errors.As, and ultimately logged only once at the appropriate boundary).

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 errors in golang without losing the original error chain?

Wrap errors in golang using fmt.Errorf with the %w verb to add context while preserving the original error for inspection. This maintains chain integrity for internal layers, allowing operators to trace failures without duplicate logs or losing the root cause.

When should I use errors.Is vs errors.As for Go error inspection?

Use errors.Is to check if an error matches a specific sentinel value, and use errors.As to extract a typed error from the chain. Correct inspection with errors.Is and errors.As ensures robust sentinel checks and typed chain extraction in production Go services.

What is the best way to handle golang errors to avoid duplicate logs in production?

The best way to handle golang errors in production is to enforce the single handling rule: log OR return the error. This prevents duplicate logs by ensuring every error is ultimately logged only once at the appropriate API boundary.

Does the samber/oops package work with slog for structured Go error logging?

Yes, samber/oops works with slog for structured Go error logging by providing production errors with stack traces and context. You use slog for structured logging while samber/oops ensures errors carry traceable context across application boundaries.

How do I combine independent failures when doing golang error handling?

To combine independent failures in golang error handling, use errors.Join to aggregate multiple errors. This groups independent failures together while maintaining the ability to inspect individual errors later using errors.Is or errors.As.

Why should golang error strings be lowercase and how does fmt.Errorf %w help?

Golang error strings should be lowercase to follow idiomatic conventions when creating and wrapping errors. Using fmt.Errorf with %w helps by adding contextual information to the lowercase string while preserving the original error chain for internal layers.