Integer overflow occurs when arithmetic produces a value outside an integer type’s representable range. Go rejects detectable constant overflow at compile time but handles run-time overflow and underflow silently, allowing surprising negative or wrapped results. section 3.2.1
Incrementing past math.MaxInt32
var counter int32 = math.MaxInt32 counter++ fmt.Printf("counter=%d\n", counter) The increment does not panic; counter becomes -2147483648. The sign bit changes because no positive value remains representable in 32 bits. section 3.2.1
Prevent silent failures by checking the operation before or after it:
- For increment, compare the value with the type limit: math.MaxInt32, math.MaxInt, or math.MaxUint. section 3.2.2
- For addition, test whether a > math.MaxInt-b before evaluating a+b. section 3.2.3
- For multiplication, handle zero, one, and math.MinInt specially, then verify that result/b still equals a. section 3.2.4
- Use math/big when an int cannot represent the required values. section 3.2.4
Compare signed and unsigned integer widths before choosing a type or its overflow boundary. section 3.2.1






