fmt.Errorf

The fmt function that creates an error with a formatted message, and wraps another error inside it when you use the %w verb.

In more detail

fmt.Errorf("host %s is unknown", host) adds useful detail to a message. fmt.Errorf("read config: %w", err) also keeps the original error inside, so errors.Is can still find it. Use %v instead of %w when callers should not depend on the cause.

Example

fmt.Errorf("start worker: %w", ErrBusy)

Related terms

Taught in

References