liveBook · Manning

%w error verb

The %w directive is a Go formatting directive that wraps an error operand as the underlying cause of a formatted error.

How 2 Manning books explain it

100 Go Mistakes and How to Avoid Them

The %w directive is a Go fmt formatting directive that records an operand as the underlying error of a returned formatted error.

How it is used
Adds context or classification while preserving an underlying error for callers and later error handling.
How deep it goes
Explains wrapping, contrasts %w with %v, and traces why errors.As replaces direct type checks after refactoring.
Examples
Uses bar failure context, permission errors, transient database failures, and HTTP 400-versus-503 responses.
What you can do after
Diagnose/debug with it by preserving causes, selecting %w or %v, and checking wrapped errors accurately.

Read how 100 Go Mistakes and How to Avoid Them explains %w error verb →

Go by Example

The %w error verb is a Go formatting directive that records an operand as the wrapped cause of a returned error.

How it is used
Uses %w in custom command-line flag parsing to add context while preserving underlying parse errors.
How deep it goes
Explains wrapping and errors.Is through a worked parser example, without descending into error implementation internals.
Examples
Demonstrates contextual errors from a custom flag parser when strconv.Atoi rejects a flag value.
What you can do after
Diagnose errors with errors.Is while retaining informative command-line parsing context.

Read how Go by Example explains %w error verb →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.