A common mistake is comparing a returned error with == after adding context. If the error wraps a sentinel with fmt.Errorf and %w, direct equality fails. errors.Is checks the requested value throughout the error chain, so wrapped errors remain matchable. section 7.4
Replace direct equality when errors may be wrapped
Problematic check:\nerr := query()\nif err != nil {\n if err == sql.ErrNoRows {\n // ...\n }\n}\n\nCorrect check:\nerr := query()\nif err != nil {\n if errors.Is(err, sql.ErrNoRows) {\n // ...\n }\n}\n\nUse == only when wrapping is not part of the error path; use errors.Is when fmt.Errorf with %w may add context. section 7.4
err := query()
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
// ...
} else {
// ...
}
}
Inspect the errors.Is replacement for a direct sentinel comparison. section 7.4


