ParseInt Enforces The bitSize Ceiling

ParseInt takes a bitSize ceiling, clamps out-of-range values to that width maximum, and returns ErrRange so 32-bit API fields stay bounded.

GoData Integrity

Code

gosnippet
18 lines
package main

import (
	"fmt"
	"strconv"
)

func main() {
	// ParseInt enforces the bitSize ceiling:64 位机器上也能模拟窄位宽接口约束
	for _, bitSize := range []int{8, 16, 32, 64} {
		v, err := strconv.ParseInt("4000000000", 10, bitSize)
		fmt.Printf("bitSize=%2d value=%d err=%v\n", bitSize, v, err)
	}

	// 越界时 err 是 ErrRange,value 被钳到该位宽的最大整数,而不是截断成垃圾值
	var limit int64 = 2147483647
	fmt.Println("int32 max:", limit)
}

Inside the snippet

ParseInt Enforces The bitSize Ceiling comes down to the function main.

Iteration happens inside for _, bitSize := range []int{8, 16, 32, 64} — everything outside it runs once.

Putting it to use

The demo output is labeled (bitSize=, int32 max:), so the printed output reads as a trace of the operations performed.

Core lines

for _, bitSize := range []int{8, 16, 32, 64} {
	v, err := strconv.ParseInt("4000000000", 10, bitSize)
	fmt.Printf("bitSize=%2d value=%d err=%v\n", bitSize, v, err)
}

Interactive Tools for This Topic

FAQ

What does ParseInt Enforces The bitSize Ceiling print when run?
Run it as a complete program — the output lines carry labels such as `bitSize=`, `int32 max:`.