为什么Golang这样在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
}

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

var cutoff = maxUint64/uint64(base) + 1

非常感谢。

解决方法

我认为您所指的comment above行可能会回答您的问题:

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

因为maxUint64/10 + 1maxUint64/16 + 1仅引用常量,编译器才可以计算该常量。结果是,每次调用ParseUint时都不需要在运行时执行除法运算。您可以在commit中查看基准。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...