Inspectopedia
 
2024.3

Direct comparison of errors

Info
Reliability
New
Last modified: 03 December 2024

Reports direct comparison of errors, for example, using ==, and suggests using errors.Is instead.

Since Go 1.13, errors can be wrapped using the fmt.Errorf function with the %w verb. Therefore, direct comparison of errors using the equality check fails on wrapped errors. The preferred way of checking for a specific error is to use the errors.Is function from the standard library as this function traverses the chain of the wrapped errors while checking for a specific error.

For more information about errors.Is, refer to working with Errors in Go 1.13.

The quick-fix replaces direct comparison of errors with a call to errors.Is.

Example:

After the quick fix is applied: