Error handling

Error wrapping and matching

Wrapping exposes an underlying error to errors.Is and errors.As. Only expose implementation errors as part of an API contract when callers may depend on them. Formatting with %v preserves text but does not wrap the error. Example output: 'true false'.

Formal Syntax & Structure

go syntaxgo
fmt.Errorf("context: %w", err)

Working Example

Example Snippetgo
package main
import "fmt"import "errors"
func main() {  e := errors.New("missing")  fmt.Println(errors.Is(fmt.Errorf("lookup: %w", e), e), errors.Is(fmt.Errorf("lookup: %v", e), e))}
Go Reference