Comparing Go errors
In a Go-based backend service, I update my dependencies. Notably, cloud.google.com/go/storage updated from v1.43.0 to v1.51.0. After deploying, I noticed a werid bug that wasn't happening before. I traced the problem to following code:
import (
// ...
gcs "cloud.google.com/go/storage"
// ...
)
// ...
if err == gcs.ErrObjectNotExist {
// ...
}
Situations that would previously trigger this conditionally, no longer were. Upon further digging, Google wants you to compare these errors using errors.Is()
. This fixed the bug:
if errors.Is(err, gcs.ErrObjectNotExist) {
// ...
}
Worried, I checked other error checking comparisons (specifically io.EOF
) and found this comment above the definition of EOF
:
// EOF is the error returned by Read when no more input is available.
// (Read must return EOF itself, not an error wrapping EOF,
// because callers will test for EOF using ==.)
So it looks like in those cases, they prepared for that scenario.