strconv.ErrRange
GoERRORNotableParsing

value out of range

Quick Answer

Use errors.Is(err, strconv.ErrRange) to detect overflow and use a wider type or reject the input.

What this means

Returned by strconv parsing functions when the numeric value is valid but exceeds the range of the target type, such as a number too large for int32.

Why it happens
  1. 1Parsing a number larger than math.MaxInt64 with strconv.ParseInt
  2. 2User-supplied value exceeds the valid range for the application field

Fix

Detect and reject

Detect and reject
_, err := strconv.ParseInt(s, 10, 32)
if errors.Is(err, strconv.ErrRange) {
    return fmt.Errorf("value out of range: %s", s)
}

Why this works

Returning an explicit error prevents silent truncation or overflow in downstream logic.

Code examples
Detectgo
_, err := strconv.ParseInt("9999999999", 10, 32)
if errors.Is(err, strconv.ErrRange) {
    fmt.Println("overflow")
}
Use 64-bitgo
n, err := strconv.ParseInt(s, 10, 64)
Clamp valuego
n, _ := strconv.ParseInt(s, 10, 64)
if n > math.MaxInt32 { n = math.MaxInt32 }
Sources
Official documentation ↗

Go standard library

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Go errors