← All notes

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

Sources

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

Permanent ID: iil:note:07sas2et

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.},
}

Markdown

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.