I am trying to compare an sql error in golang using errors.Is(...) method, which is always returning false.
I have declared an error type variable as below.
var ErrNoRows = errors.New("no rows in result set")
Somewhere down the flow, I am trying to compare an error value with the 'ErrNoRows' error, which always returns false.
However, if I print the err in debug mode, it prints as..
(dlv) p err
error(*errors.errorString) *{
s: "no rows in result set",}
and printing ErrNoRows also returns the same thing.
(dlv) p ErrNoRows
error(*errors.errorString) *{
s: "no rows in result set",}
But both these objects are not getting equal, when I compare them with Error.Is function.
err := tx.QueryRow(ctx, sqlstmt).Scan(&myvar)
if errors.Is(err, ErrNoRows) {
...
}
The above check always fails. What am I doing wrong here? I am using https://github.com/jackc/pgx for sql operation.
EDIT I have already seen this question (How to compare Go errors)
The above question talks about using errors.Is(...), instead of a naive equal comparision. I followed the advice as per that question thread.
I was getting error because of not knowing that I still had to deal with the same instances of error objects, as was clarified by @mkopriva.