go-errors
Skill0 starsChanged 5 months agoMITView on GitHub
Idiomatic Go error handling. This skill should be used when the user writes, reviews, or debugs Go error code — wrapping with `%w`, `errors.Is`/`errors.AsType`, sentinel vs typed errors, `errors.Join`, an unchecked `Close`, when panic is legitimate, `Must` helpers, enum-switch dispatch defaults, keeping payload values out of boundary errors and logs, or chasing a swallowed or context-losing error. Pair with the `errorlint` linter. Go only.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- CasLubbers/code-design-skillstheir own version, first seen Aug 25, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- CasLubbers/code-design-skills★ 2same 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 repository9 other files it gives its agents.
- AGENTS.mdAGENTS.md
- .claude/CLAUDE.mdCLAUDE.md
- .github/copilot-instructions.mdCopilot instructions
- go-codingSkill
- go-concurrencySkill
- go-idiomsSkill
- go-layoutSkill
- go-lint-setupSkill
- go-testingSkill
The file
Files
go-errors — Go error handling
Deterministic backstop: golangci-lint run --enable-only=errorlint, plus errcheck (in the
standard set) for unchecked errors. Run the tool first; this skill is the judgment around it.
Rules
- Wrap with
%wwhen the caller may need to inspect the cause (Go 1.13):return fmt.Errorf("read config %s: %w", path, err). Use%vonly to deliberately sever the chain (e.g. to avoid leaking an internal error type across an API boundary) — and say so; anywhere else%vdiscards the chain and downstreamerrors.Is/errors.Assilently fail (errorlintflags it). Put%wlast so the message reads outside-in; a leading%wis right only when the sentinel is the sentence:fmt.Errorf("%w: %s", ErrNotFound, key). - Inspect with
errors.Is(sentinel) /errors.AsType[E](typed) — nevererr == ErrXor a type assertion once any layer wraps, or the result is sentinel breakage (the comparison silently stops matching).if perr, ok := errors.AsType[*fs.PathError](err); ok { … }(Go 1.26): the generic form — compile-time-checked target, no pointer to prepare, no reflection, cannot panic on a mistyped target.errors.As(Go 1.13) is not deprecated and existing call sites are not bugs —go fix(errorsastype) converts them. - Don't
%wa pointer to an error type with value receivers.fmt.Errorf("…: %w", p)wherepis a*EandEitself implementserrorputs a*Ein the chain, soerrors.Isand type checks forEmiss it; wrap*p. A pointer-receiver error such as*fs.PathErroris wrapped as the pointer, as usual.go vet(printf) reports the mistake from Go 1.27. - Sentinel errors (
var ErrNotFound = errors.New("not found")) for expected, comparable conditions that are part of the API contract — keep the set small and documented. Typed errors (a struct implementingerror) when callers need fields (*PathError). errors.Join(err1, err2)(Go 1.20) to aggregate independent failures (cleanup, validation) — replaces manual concatenation and most third-party multierror use.- Never swallow: no
_ = f()on an error that matters; no emptyif err != nil {}. Handle, wrap-and-return, or (deliberately, with a comment) ignore. - Check
Closeon anything written to.defer f.Close()discards a failed flush — the write looks successful and the file is truncated. Capture it into a named result:defer func() { err = errors.Join(err, f.Close()) }().errcheckflags the discarded form; read-only handles are the one safe place to drop it (say so with_ =). - Keep the happy path at minimal indentation — handle the error and return early; no
elseafter a terminatingif. Error flow goes in the indented branch, business logic does not. - Don't panic across a package boundary. Errors are the mechanism for anything a caller can
plausibly hit; panic is for programmer error, API misuse, and genuinely unreachable states. If a
package uses panic internally for unwinding,
recoverit inside that package and return an error — a panic must never escape into a caller. MustXis for package initialisation and test helpers, not for input. A helper that stops the program on failure carries theMustprefix (regexp.MustCompile,template.Must) and is called while setting up package-level values from constants the author controls; the same prefix fits a test helper that stops only the current test witht.Fatal(mustParse(t, s)). Anything that can fail on user input, a file, or the network returns an error instead — aMuston a request path turns bad input into a crash.- Fail loudly on impossible dispatch: a
switchover an internal enum/kind gets adefaultthat returns an error (panic only for the genuinely unreachable) — never a silent pass-through that lets a later-added member ride the weakest arm. Pin exhaustiveness with theexhaustivelinter (enabled in the reference config) or a completeness test that iterates the enum. - Boundary errors carry classification, not payload: upstream messages can embed data values —
a database driver quoting the offending stored value, a validator echoing the request body. At a
logging or API boundary, pass the stable class/code (e.g. SQLSTATE) and keep the raw message
internal — the message-content analogue of severing an internal error type with
%v. - Add context at each layer, log once at the boundary. Wrapping at every level and logging at
every level produces duplicate noise — return wrapped, log at the top. Keep the added context
terse:
"new store: %w", not"failed to create new store: %w"— "failed to" states the obvious and piles up (failed to x: failed to y: …) as the error climbs the stack. - Error strings: lowercase, no trailing punctuation (they get wrapped):
"cannot parse %q".
Sources
- Go 1.13 errors — https://go.dev/blog/go1.13-errors;
errors.AsType(Go 1.26) — https://pkg.go.dev/errors#AsType;printfvet check on%wof*E— https://go.dev/doc/go1.27 - Code Review Comments (Error Strings, Handle Errors, Indent Error Flow, Don't Panic) — https://go.dev/wiki/CodeReviewComments
- Google Go Style Decisions (Must functions, Returning errors, Error strings, Handle errors, In-band errors, Don't panic) — https://google.github.io/styleguide/go/decisions; Best Practices (Error handling, Panics,
%wplacement) — https://google.github.io/styleguide/go/best-practices - Uber Go Style Guide (Errors) — https://github.com/uber-go/guide
os.File.Closereturns write errors — https://pkg.go.dev/os#File.Close
Decomposition inspired by samber/cc-skills-golang (MIT © 2026 Samuel Berthe); rules grounded in the sources above.
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.

