pkg/errors is a Go library that provides primitives for creating, annotating, and inspecting errors with stack traces and contextual information. Its core identity centers on capturing the call stack at the point of error creation or wrapping, preserving that information for later debugging and root cause analysis.
The library distinguishes itself through a set of tightly integrated capabilities for error handling. It supports wrapping errors with descriptive messages while recording a new stack trace at the wrapping point, annotating existing errors with either a message or a stack trace independently, and creating new errors that automatically capture the call stack. For inspection, it provides mechanisms to walk the wrapped error chain to retrieve the root underlying cause or to search through the chain for an error matching a specific type or value.
Error formatting is handled through Go's standard formatting verbs, allowing developers to conditionally print stack trace details alongside error messages for diagnostic purposes. The library's design forms a linked chain of wrapped errors, enabling traversal from the outermost context down to the original error.