go-error-handling
Skill9 starsChanged 4 months agoMITView on GitHub
Use when writing, wrapping, inspecting, or logging Go errors. Covers strategy choice (sentinel vs typed vs opaque), wrapping with %w/%v, errors.Is/As/Join, the log-or-return rule, error strings, and panic/recover boundaries. Apply proactively whenever a function returns or accepts an error, even if the user has not asked about error handling.
3 other projects use a version of this.
Adopted by
Most recently taken up by
- eduardo-sl/go-agent-skillstheir own version, first seen Aug 18, 2026
- cxuu/golang-skillstheir own version, first seen Jun 20, 2026
- saisudhir14/claude-skillstheir own version, first seen Mar 25, 2026
Other versions3 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 3
- cxuu/golang-skills★ 165same name
- eduardo-sl/go-agent-skills★ 72same name
- saisudhir14/claude-skills★ 9same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository26 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- go-clean-architectureSkill
- go-code-reviewSkill
- go-code-styleSkill
- go-concurrencySkill
- go-contextSkill
- go-control-flowSkill
- go-databaseSkill
- go-data-structuresSkill
- go-declarationsSkill
- go-defensiveSkill
- go-documentationSkill
- go-functional-optionsSkill
- go-functionsSkill
- go-genericsSkill
- go-graphqlSkill
- go-grpcSkill
- go-interfacesSkill
- go-lintingSkill
- go-loggingSkill
- go-namingSkill
- go-observabilitySkill
- go-packagesSkill
- go-performanceSkill
- go-swaggerSkill
- go-testingSkill
The file
We split this skill into the rules it gives an agent
2 rules. 0 are common, 1 is distinctive, and 1 is very specific to this project.
0
Common
Most of the 3 edited versions we read keep these.
1
Distinctive
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 156
Do not use recover to convert panics into errors as normal control flow.
- Distinctive: line 173
Before finishing an error-handling change:
“Common” is measured: kept by at least 6 in 10 of the 3 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Go Error Handling
Errors in Go are values. Treat them as part of the API: choose a strategy per failure mode, propagate with wrapping, inspect with errors.Is/As, and handle each error exactly once.
Core Rules
- Errors are values, not exceptions. Return them; do not panic across API boundaries.
- Handle each error exactly once. Either log it or return it — never both.
- The caller decides what is exceptional. Library code returns; binaries (or top-level handlers) decide whether to log, retry, or exit.
- Wrap only when you add real context. A wrap that just repeats the function name is noise. Use
%wto preserve identity;%vto deliberately hide an unstable type.
Strategy Decision
Pick the simplest strategy that meets the caller's needs:
| Strategy | When to use | Example |
| Opaque error (default) | Caller only needs to know something failed | errors.New("invalid input") |
| Sentinel error | Caller needs to test for a specific named condition | io.EOF, sql.ErrNoRows |
| Typed error | Caller needs structured fields (path, code, retry-after) | *os.PathError, *url.Error |
| Joined errors | A single operation produced several independent failures | errors.Join(errA, errB) |
Read references/strategy-decision.md when the caller's needs are unclear or when migrating between strategies without breaking callers.
Writing Errors
Strings
- Lowercase, no trailing punctuation. Errors are composed:
fmt.Errorf("write %s: %w", path, err)reads as one sentence. - Be specific.
"open config: permission denied"beats"failed to open file". - Do not include the function name. Stack context is added by wrapping at each layer.
Creating
// Opaque — the caller only checks != nil
return errors.New("invalid character in token")
// Sentinel — exported, package-level, named ErrXxx
var ErrNotFound = errors.New("user: not found")
// Typed — when callers need structured fields
type ValidationError struct {
Field string
Rule string
}
func (e *ValidationError) Error() string {
return fmt.Sprintf("validation: %s violates %s", e.Field, e.Rule)
}Wrapping and Inspection
Wrap with %w to add context while preserving identity
if err := db.Get(id); err != nil {
return fmt.Errorf("loading user %d: %w", id, err)
}Inspect with errors.Is (identity) and errors.As (type)
if errors.Is(err, sql.ErrNoRows) { /* handled */ }
var ve *ValidationError
if errors.As(err, &ve) {
return reply.BadRequest(ve.Field)
}Never compare error strings (err.Error() == "...") — strings are not stable API.
Join independent failures
errs := errors.Join(
validate(name),
validate(email),
validate(password),
)
if errs != nil {
return errs // errors.Is/As walks both branches
}Read references/wrapping-vs-shadowing.md when deciding between
%w(expose) and%v(hide), or when wrapping would leak an implementation detail.
Error Flow
Log or return — not both
// Bad: caller will log it again, producing duplicate lines
if err := svc.Do(ctx); err != nil {
slog.ErrorContext(ctx, "svc.Do failed", "err", err)
return err
}
// Good: log only at the boundary that decides the request is done
if err := svc.Do(ctx); err != nil {
return fmt.Errorf("doing svc work: %w", err)
}The HTTP handler / job runner / main is the only layer that logs.
Reduce nesting with guard clauses
// Bad
if err == nil {
if x, ok := f(); ok {
return x, nil
}
}
return zero, err
// Good
if err != nil {
return zero, err
}
x, ok := f()
if !ok {
return zero, errSomething
}
return x, nilPanic and Recover
panic is for programmer errors (impossible states) and package initialization. It is never the right way to return a normal failure.
// Acceptable: invariants the type guarantees
func (q *Queue) MustEnqueue(v T) { if err := q.Enqueue(v); err != nil { panic(err) } }
// Acceptable: recover at the goroutine boundary so one bad request cannot kill the server
defer func() {
if r := recover(); r != nil {
log.Error("panic recovered", "value", r, "stack", debug.Stack())
http.Error(w, "internal error", 500)
}
}()Do not use recover to convert panics into errors as normal control flow.
For custom error types, implement Unwrap() error (or Unwrap() []error in Go 1.20+) so errors.Is/As can reach the cause. See references/wrapping-vs-shadowing.md.
Anti-Patterns
| Anti-pattern | Why it hurts | Do this instead |
return errors.New(err.Error()) | Drops identity; errors.Is breaks | return fmt.Errorf("ctx: %w", err) |
if err.Error() == "EOF" | String matching against unstable text | errors.Is(err, io.EOF) |
_ = doThing() | Silently swallows failures | Handle, log at boundary, or document why |
Returning *MyError (concrete pointer) | Typed-nil trap; non-nil interface | Return error (see references/typed-nil-trap.md) |
| Logging then returning the same error | Duplicate log lines, no single source of truth | Log only at the top boundary |
| Wrapping at every layer with no new info | "a: b: c: d: real error" chains | Drop the wrap and return err |
Verification Checklist
Before finishing an error-handling change:
- No
err.Error()string comparisons - All wrapping uses
%w(or%vis intentional and commented) - Functions return the
errorinterface, not concrete types - Each error is logged at most once (at the request/job boundary)
- Sentinels are package-level
var ErrXxx = errors.New(...) - Typed errors expose only the fields callers actually need
References
- references/strategy-decision.md — picking opaque vs sentinel vs typed
- references/wrapping-vs-shadowing.md —
%wvs%vdecisions - references/typed-nil-trap.md — why returning
*MyErrbreaks== nil
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

