在 Golang 中使用类型断言检测值超出范围错误
Detect value out of range errors using type assertion in Golang
给定以下代码:
iv, err := strconv.ParseInt("18446744073709551448", 10, 64)
fmt.Println(iv)
fmt.Printf("%#v\n", err)
fmt.Printf("%v\n", err)
//Output:
9223372036854775807
&strconv.NumError{Func:"ParseInt", Num:"18446744073709551448", Err:(*errors.errorString)(0x1040a040)}
strconv.ParseInt: parsing "18446744073709551448": value out of range
如何检测函数因超出 int64 范围而失败? strconv.ParseInt function returns an error type, but in this case it is actually a strconv.NumError type as indicated by %#v
. The Error Handling and Go 文章提到您可以使用类型断言来检查特定类型的错误,但它没有给出任何示例。我应该用什么表达式来完成这段代码:
if expression {
uv, err := strconv.ParseUint("18446744073709551448", 10, 64)
}
从 strconv.ParseInt
返回的错误仅在编译时才知道是某种实现 Error 接口的类型。类型断言允许您坚持认为它是一个 strconv.NumError
并直接检查它的字段,但如果您发现错误,则有引发运行时恐慌的风险:
if err.(*strconv.NumError).Err.Error() == "value out of range" {
uv, err := strconv.ParseUint("18446744073709551448", 10, 64)
}
一个更灵活的解决方案(但对于您的目的来说可能过于松散)是在 err.Error()
方法上执行子字符串匹配:
if strings.Contains(err.Error(), "value out of range") {
uv, err := strconv.ParseUint("18446744073709551448", 10, 64)
}
我们有,
var ErrRange = errors.New("value out of range")
ErrRange indicates that a value is out of range for the target type.
type NumError struct {
Func string // the failing function (ParseBool, ParseInt, ParseUint, ParseFloat)
Num string // the input
Err error // the reason the conversion failed (ErrRange, ErrSyntax)
}
A NumError records a failed conversion.
func (e *NumError) Error() string
例如,
package main
import (
"fmt"
"strconv"
)
func main() {
iv, err := strconv.ParseInt("18446744073709551448", 10, 64)
if err != nil {
if numError, ok := err.(*strconv.NumError); ok {
if numError.Err == strconv.ErrRange {
fmt.Println("Detected", numError.Num, "as a", strconv.ErrRange)
return
}
}
fmt.Println(err)
return
}
fmt.Println(iv)
}
输出:
Detected 18446744073709551448 as a value out of range
给定以下代码:
iv, err := strconv.ParseInt("18446744073709551448", 10, 64)
fmt.Println(iv)
fmt.Printf("%#v\n", err)
fmt.Printf("%v\n", err)
//Output:
9223372036854775807
&strconv.NumError{Func:"ParseInt", Num:"18446744073709551448", Err:(*errors.errorString)(0x1040a040)}
strconv.ParseInt: parsing "18446744073709551448": value out of range
如何检测函数因超出 int64 范围而失败? strconv.ParseInt function returns an error type, but in this case it is actually a strconv.NumError type as indicated by %#v
. The Error Handling and Go 文章提到您可以使用类型断言来检查特定类型的错误,但它没有给出任何示例。我应该用什么表达式来完成这段代码:
if expression {
uv, err := strconv.ParseUint("18446744073709551448", 10, 64)
}
从 strconv.ParseInt
返回的错误仅在编译时才知道是某种实现 Error 接口的类型。类型断言允许您坚持认为它是一个 strconv.NumError
并直接检查它的字段,但如果您发现错误,则有引发运行时恐慌的风险:
if err.(*strconv.NumError).Err.Error() == "value out of range" {
uv, err := strconv.ParseUint("18446744073709551448", 10, 64)
}
一个更灵活的解决方案(但对于您的目的来说可能过于松散)是在 err.Error()
方法上执行子字符串匹配:
if strings.Contains(err.Error(), "value out of range") {
uv, err := strconv.ParseUint("18446744073709551448", 10, 64)
}
我们有,
var ErrRange = errors.New("value out of range")
ErrRange indicates that a value is out of range for the target type.
type NumError struct { Func string // the failing function (ParseBool, ParseInt, ParseUint, ParseFloat) Num string // the input Err error // the reason the conversion failed (ErrRange, ErrSyntax) }
A NumError records a failed conversion.
func (e *NumError) Error() string
例如,
package main
import (
"fmt"
"strconv"
)
func main() {
iv, err := strconv.ParseInt("18446744073709551448", 10, 64)
if err != nil {
if numError, ok := err.(*strconv.NumError); ok {
if numError.Err == strconv.ErrRange {
fmt.Println("Detected", numError.Num, "as a", strconv.ErrRange)
return
}
}
fmt.Println(err)
return
}
fmt.Println(iv)
}
输出:
Detected 18446744073709551448 as a value out of range