Skip to content

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