fmt.Println("见证奇迹的时刻到了,(1 + 8) / 2 = ", (1+8)/2)
golang 里 untyped contants 的运算结果类型确定方式如下(https://golang.org/ref/spec#C...:
Any other operation on untyped constants results in an untyped constant of the same kind; that is, a boolean, integer, floating-point, complex, or string constant. If the untyped operands of a binary operation (other than a shift) are of different kinds, the result is of the operand's kind that appears later in this list: integer, rune, floating-point, complex. For example, an untyped integer constant divided by an untyped complex constant yields an untyped complex constant.
这里大家都是 integer ,所以结果也是 integer.
注意对于有类型的,规则还不太一样(https://golang.org/ref/spec#A...:
Arithmetic operators apply to numeric values and yield a result of the same type as the first operand.
2 回答1.4k 阅读
2 回答1.5k 阅读
2 回答1.3k 阅读
2 回答1.3k 阅读
3 回答900 阅读
3 回答1.1k 阅读
2 回答1.1k 阅读
go:整数除以浮点数的问题
为什么你会有“两个 int 类型相除,结果会自动转换为 float ”的想法。