Values
值
Why does Go not provide implicit numeric conversions?
为什么 Go 不提供隐式数值转换?
The convenience of automatic conversion between numeric types in C is outweighed by the confusion it causes. When is an expression unsigned? How big is the value? Does it overflow? Is the result portable, independent of the machine on which it executes? It also complicates the compiler; “the usual arithmetic conversions” are not easy to implement and inconsistent across architectures. For reasons of portability, we decided to make things clear and straightforward at the cost of some explicit conversions in the code. The definition of constants in Go—arbitrary precision values free of signedness and size annotations—ameliorates matters considerably, though.
C 中数字类型之间自动转换的便利性被它引起的混乱所抵消。当表达式是无符号类型时?值有多大?会溢出吗?结果是否可移植,独立于执行它的机器?它还使编译器变得复杂;“通常的算术转换”不容易实现,并且在不同架构之间不一致。出于可移植性的原因,我们决定让事情变得清晰和直接,但代价是在代码中进行一些显式转换。不过,Go 中常量的定义——不受符号和大小注释影响的任意精度值——大大改善了问题。
outweighed
v. 在重量上超过( outweigh的过去式和过去分词 );在重要性或价值方面超过;
A related detail is that, unlike in C, int
and int64
are distinct types even if int
is a 64-bit type. The int
type is generic; if you care about how many bits an integer holds, Go encourages you to be explicit.
一个相关的细节是,与 C 不同, 即使是在64位系统,int
和int64
也是不同的类型。int
类型是通用的;如果你关心一个整数包含多少位,Go 鼓励你明确地表达出来。
How do constants work in Go?
常量在 Go 中如何工作?
Although Go is strict about conversion between variables of different numeric types, constants in the language are much more flexible. Literal constants such as 23
, 3.14159
and math.Pi
occupy a sort of ideal number space, with arbitrary precision and no overflow or underflow. For instance, the value of math.Pi
is specified to 63 places in the source code, and constant expressions involving the value keep precision beyond what a float64
could hold. Only when the constant or constant expression is assigned to a variable—a memory location in the program—does it become a "computer" number with the usual floating-point properties and precision.
尽管 Go 对不同数值类型的变量之间的转换有严格要求,但Go语言中的常量要灵活得多。诸如23
、3.14159
和 之类的文字常量math.Pi
占据一种理想的数字空间,具有任意精度并且没有上溢或下溢。例如,math.Pi
源代码中的值被指定为 63 个位置,并且涉及该值的常量表达式的精度超出了 afloat64
所能容纳的精度。只有当常量或常量表达式被分配给变量(程序中的内存位置)时,它才会成为具有通常的浮点属性和精度的“计算机”数字。
Literal
adj. 照字面的;原义的;逐字的;平实的,避免夸张;】
ideal
adj. 理想的,完美的;被认为是最好的;完全或相当令人满意的;想象的,假设的;
n. 理想;典范,典型;目标,高尚的、有价值的原则或目标;
arbitrary
adj. 武断的,任意的;专制的;
Also, because they are just numbers, not typed values, constants in Go can be used more freely than variables, thereby softening some of the awkwardness around the strict conversion rules. One can write expressions such as
此外,由于它们只是数字,而不是键入的值,因此 Go 中的常量比变量可以更自由地使用,从而缓解了严格转换规则带来的一些尴尬。人们可以写出诸如这样的表达式
softening
n. 变软,软化;
v. (使)变软( soften的现在分词 );缓解打击;缓和;安慰;
awkwardness
n. 笨拙;粗劣;难为情;尴尬;
sqrt2 := math.Sqrt(2)
without complaint from the compiler because the ideal number 2
can be converted safely and accurately to a float64
for the call to math.Sqrt
.
编译器不会抱怨,因为理想的数字2 可以安全准确地转换为 afloat64以便调用math.Sqrt.
A blog post titled Constants explores this topic in more detail.
题为“常量” 的博客文章 更详细地探讨了这个主题。