Go "constant overflows type" in CI
Go checks at compile time that a constant fits the type it is being assigned to. A value outside the type range (for example 300 into an int8) is rejected as "overflows".
What this error means
Compilation fails with "constant 300 overflows int8" (or uint8, int16, and so on) at an assignment or conversion.
go build
./limits.go:5:18: constant 300 overflows int8Common causes
The constant is larger than the type holds
A sized integer type like int8 (max 127) cannot hold the literal, so the compiler refuses the assignment.
A negative literal assigned to an unsigned type
Assigning a negative constant to a uint-family type overflows because unsigned types start at zero.
How to fix it
Widen the type or correct the value
- Decide whether the value or the type is wrong.
- Use a wider integer type, or change the constant to fit the intended type.
- Rebuild to confirm the constant fits.
limits.go
var max int16 = 300 // int16 holds up to 32767Use an unsigned type only for non-negative values
Reserve uint types for values that are never negative; otherwise use a signed type.
How to prevent it
- Pick integer widths that comfortably hold the expected range.
- Be deliberate when assigning literals to sized integer types.
- Let CI compile the package so overflowing constants are caught.
Related guides
Go "invalid operation: mismatched types" in CIFix Go "invalid operation: x + y (mismatched types int and string)" in CI - a binary operator was applied to…
Go "cannot use x (variable of type T) as U value in argument" in CIFix Go "cannot use x (variable of type T) as U value in argument to f" in CI - a value is passed to a functio…
Go "cannot range over n (variable of type int)" on an older toolchain in CIFix Go "cannot range over n (variable of type int)" in CI - ranging over an integer requires Go 1.22 or newer…