猿问

如何使用 errors.Is 检查错误是否为 strconv.NumError

我有这个错误

错误是 ParseInt 类型。如何检查我假设我会使用errors.Is但不确定在这种情况下我将如何做的错误



慕沐林林
浏览 73回答 1
1回答

智慧大石

https://pkg.go.dev/strconv@go1.19.3#NumErrortype NumError struct {    Func string // the failing function (ParseBool, ParseInt, ParseUint, ParseFloat, ParseComplex)    Num  string // the input    Err  error  // the reason the conversion failed (e.g. ErrRange, ErrSyntax, etc.)}错误是 ParseInt 类型。"ParseInt"是“失败函数”的名称,即返回错误的函数。实际的错误类型是*strconv.NumError。您可以像这样检查它和 func 名称:if e, ok := err.(*strconv.NumError); ok && e.Func == "ParseInt" {    // do xyz}
随时随地看视频慕课网APP

相关分类

Go
我要回答