How to compare Go errors

Issue

I have an error value which when printed on console gives me Token is expired

How can I compare it with a specific error value? I tried this but it did not work:

if err == errors.New("Token is expired") {
      log.Printf("Unauthorised: %s\n", err)
}

Solution

Try

err.Error() == "Token is expired"

Or create your own error by implementing the error interface.

Answered By – Sridhar

Answer Checked By – Marie Seifert (GoLangFix Admin)

Leave a Reply

Your email address will not be published.