为什么 Golang 在 atoi.go 中像这样处理截止?

lit*_*lec 3 overflow atoi go

// code in atoi.go, line 90
var cutoff uint64
switch base {
case 10:
    cutoff = maxUint64/10 + 1
case 16:
    cutoff = maxUint64/16 + 1
default:
    cutoff = maxUint64/uint64(base) + 1
}
Run Code Online (Sandbox Code Playgroud)

我在Golang包的atoi.go文件中看到了一些代码,为什么不像下面这样写呢?

var cutoff = maxUint64/uint64(base) + 1
Run Code Online (Sandbox Code Playgroud)

非常感谢。

Bri*_*its 6

我认为您所指的行上方评论可能会回答您的问题:

// 在常见情况下使用编译时常量。

因为maxUint64/10 + 1并且maxUint64/16 + 1只有引用常量编译器才能计算出来。结果是每次ParseUint调用都不需要在运行时进行除法运算。您可以在提交中看到基准测试。