liveBook · Manning

%v directive

As explained in 100 Go Mistakes and How to Avoid Them

The %v formatting directive formats an error as text without preserving the original error for unwrapping.

A common mistake is replacing %w with %v while assuming callers can still inspect the original error. %v converts the error to text and adds context, but the source error is no longer available for unwrapping. section 7.2

Convert instead of wrap

if err != nil { return fmt.Errorf("bar failed: %v", err) } The message retains information about the failure, but callers cannot unwrap the returned error or check whether the source was a particular error type or value. section 7.2

Use %v when the underlying error is an implementation detail and should not become part of the caller-facing contract. Use %w when callers should inspect or unwrap the source error. section 7.2

if err != nil {
    return fmt.Errorf("bar failed: %v", err)
}

Inspect the concrete fmt.Errorf call that converts an error with %v. section 7.2

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Explains conversion, lost unwrapping, and API-coupling tradeoffs through a concrete fmt.Errorf example.
Examples
Uses fmt.Errorf("bar failed: %v", err) to show contextual conversion without source-error access.
What it assumes
Assumes familiarity with Go errors and introduces the distinction between wrapping and converting them.
What you can do after
Evaluate whether an error should preserve its underlying identity or expose only transformed context.
How it connects
Contrasts error wrapping, the %w directive, custom error types, and error unwrapping.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Go Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.