Skip to content

Use errors.Is for error comparison in assertError helper #827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tech-sam
Copy link

@tech-sam tech-sam commented Dec 3, 2024

What problem are you trying to solve?

The current assertError helper function in the Maps chapter uses direct error comparison (!=) which doesn't handle wrapped errors correctly. This could teach readers a pattern that might not work in real-world scenarios where error wrapping is common.

What changes are you making?

Updated the assertError helper function to use errors.Is instead of direct comparison. This better reflects modern Go error handling practices and will work correctly with wrapped errors.

Example

Before:

if got != want {
    t.Errorf("got error %q want %q", got, want)
}

After:

if !errors.Is(got, want) {
    t.Errorf("got error %q want %q", got, want)
}

Additional context

errors.Is was introduced in Go 1.13 and is now the recommended way to compare errors. This change helps teach more robust error handling patterns.
If this change is approved, I will create a corresponding update to the README file to explain why errors.Is is preferred over direct comparison and provide examples of when it's particularly useful (such as with wrapped errors).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant