Go error handling: error values, wrapping, errors.Is and As
Short answer
Go functions return an error as the last result; check if err != nil right away. Wrap with fmt.Errorf("...: %w", err) and inspect with errors.Is (compare to a value) or errors.As (get a type). [S1][S2]
Example
f, err := os.Open(name)
if err != nil {
return fmt.Errorf("open config: %w", err)
}
Why
error is just an interface with an Error() string method, so any type can carry extra detail. [S1][S2]
Claims
- id: C1 claim: Go uses error values to indicate abnormal state. source: S1 evidence: "Go code uses error values to indicate an abnormal state."
- id: C2 claim: The error type is an interface. source: S1 evidence: "The error type is an interface type."
- id: C3 claim: Go 1.13 added errors.Is and errors.As. source: S2 evidence: "includes two new functions for examining errors: Is and As"
- id: C4 claim: errors.Is compares an error to a value. source: S2 evidence: "function compares an error to a value"
Sources
- [S1] Error handling and Go - The Go Programming Language. The Go Authors (go.dev). https://go.dev/blog/error-handling-and-go . License: CC BY 4.0 (go.dev text). Accessed 2026-11-04.
- [S2] Working with Errors in Go 1.13 - The Go Programming Language. The Go Authors (go.dev). https://go.dev/blog/go1.13-errors . License: CC BY 4.0 (go.dev text). Accessed 2026-11-04.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Go Authors (go.dev), Error handling and Go - The Go Programming Language, CC BY 4.0 (go.dev text); The Go Authors (go.dev), Working with Errors in Go 1.13 - The Go Programming Language, CC BY 4.0 (go.dev text).
Cite this
iinventedlight. 2026. "Go error handling: error values, wrapping, errors.Is and As." iinventedlight Library, iil:note:07sas2et, version of 2026-11-04. https://iinventedlight.com/library/notes/go-error-handling/. License: CC-BY-4.0.
@misc{iil_note_07sas2et,
author = {{iinventedlight}},
title = {{Go error handling: error values, wrapping, errors.Is and As}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/go-error-handling/},
note = {Permanent ID iil:note:07sas2et. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/go-error-handling.md.