errors.Is and pointer arguments

79 views
Skip to first unread message

Arve Knudsen

unread,
Nov 27, 2020, 8:46:36 AM11/27/20
to golang-nuts
Hello!

I've noticed that errors.Is returns false if you compare two error instances that are implemented as pointers. I.e., I'm using a struct where the Error method has a pointer receiver, and when passing struct instance pointers to errors.Is, it ends up comparing them by pointer address. Please see my example to see what I mean.

Is this a bug in errors.Is or is it actually supposed to work this way? It really took us by surprise, and we ended up having to use errors.As instead since it does handle this case.

For reference, I've already made a patch to errors.Is that changes it to support pointer arguments :)

Best,
Arve

Arve Knudsen

unread,
Nov 27, 2020, 8:55:05 AM11/27/20
to golang-nuts
Actually, never mind about this. I realized in the meantime that I shouldn't be using `errors.Is` in this case, where I basically want to see if the error is of a certain type. `errors.As` makes more sense for this use case.

Jan Mercl

unread,
Nov 27, 2020, 9:01:34 AM11/27/20
to Arve Knudsen, golang-nuts

Arve Knudsen

unread,
Nov 27, 2020, 9:21:05 AM11/27/20
to Jan Mercl, golang-nuts
Thanks, Jan, but the error type in question is 3rd party so can't be extended with Is() :)

Best,
Arve
Reply all
Reply to author
Forward
0 new messages