Go语言爱好者周刊:第 86 期

polaris · · 7269 次点击
RRCAT920
真的猛士,敢于直面惨淡的人生,敢于正视淋漓的鲜血。
C:49 常量会随着上下文改变
#2
更多评论
* [The Go Programming Language Specificatio - Representability](https://golang.org/ref/spec#Representability) > A constant x is representable by a value of type T if one of the following conditions applies: > > x is in the set of values determined by T. T is a floating-point type and x can be rounded to T's precision without overflow. Rounding uses IEEE 754 round-to-even rules but with an IEEE negative zero further simplified to an unsigned zero. Note that constant values never result in an IEEE negative zero, NaN, or infinity. T is a complex type, and x's components real(x) and imag(x) are representable by values of T's component type (float32 or float64). * [The Go Blog - Constants](https://blog.golang.org/constants) > The concept of untyped constants in Go means that all the numeric constants, whether integer, floating-point, complex, or even character values, live in a kind of unified space. It's when we bring them to the computational world of variables, assignments, and operations that the actual types matter. But as long as we stay in the world of numeric constants, we can mix and match values as we like. > A constant may be given a type explicitly by a constant declaration or conversion, or implicitly when used in a variable declaration or an assignment or as an operand in an expression. It is an error if the constant value cannot be represented as a value of the respective type. > An untyped constant has a default type which is the type to which the constant is implicitly converted in contexts where a typed value is required, for instance, in a short variable declaration such as i := 0 where there is no explicit type. The default type of an untyped constant is bool, rune, int, float64, complex128 or string respectively, depending on whether it is a boolean, rune, integer, floating-point, complex, or string constant. 无类型常量(untyped constant)2.0被转为rint32类型值2; (int32)98/(int32)2 = (int32)49
#1