Skip to content
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

errors: feature request: nil-safe compares #6965

Closed
happygiraffe opened this issue Dec 16, 2013 · 3 comments
Closed

errors: feature request: nil-safe compares #6965

happygiraffe opened this issue Dec 16, 2013 · 3 comments

Comments

@happygiraffe
Copy link

@happygiraffe happygiraffe commented Dec 16, 2013

Frequently when writing tests, we end up comparing two error values, on of which may be
nil.  It would be useful to have an errors.Equal() function, which can execute a
nil-safe comparison.  This is the function, which we've been copy-and-pasting around our
test packages:

func Equal(a, b error) bool {
    if a == nil {
        return b == nil
    }
    return b != nil && a.Error() == b.Error()
}
@happygiraffe
Copy link
Author

@happygiraffe happygiraffe commented Dec 16, 2013

Comment 1:

A change to implement this is at https://golang.org/cl/42790044.
@robpike
Copy link
Contributor

@robpike robpike commented Dec 16, 2013

Comment 2:

Special definitions of equality do not belong in the standard library. You can always
provide such things in your own package. As you demonstrate, it's easy. a==b is easy too.

Status changed to WorkingAsIntended.

@happygiraffe
Copy link
Author

@happygiraffe happygiraffe commented Dec 16, 2013

Comment 3:

It's the nil == a.Error() that's less easy.
But, as iant pointed out in golang-dev, reflect.DeepEqual() should cover that.
@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.