Inspectopedia Help

Direct comparison of errors

Reports direct comparison of errors, e.g. with ==, 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 may fail on wrapped errors. The preferred way for checking for a specific error since Go 1.13 is to use the errors.Is function from the standard library.

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


if err == MyError { fmt.Println("MyErr") }

After the quick fix is applied:

if errors.Is(err, MyError) { fmt.Println("MyErr") }

Inspection Details

Available in:

GoLand 2023.3



Last modified: 13 July 2023