如何比较 Go 错误 [英] How to compare Go errors
本文介绍了如何比较 Go 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个错误值,当在控制台上打印时给我 Token is expired
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
", err)
}
推荐答案
尝试
err.Error() == "Token is expired"
或者通过实现错误接口来创建自己的错误.
Or create your own error by implementing the error interface.
这篇关于如何比较 Go 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文