题目:
以下代码输出什么?
1 2 3 4 5 6 7 8 9 10
| package main
import ( "fmt" )
func main() { var a, b float64 = 1.0, 4.0 fmt.Println(a | b) }
|
A:5;B:+Inf;C:panic;D:不能编译
答案解析:
正确答案:D
|
操作是按位或操作符,它的操作数只能是整数,而上面这道题的操作数是 float64,因此编译不通过。
这是 Go 规范的内容 https://docs.studygolang.com/ref/spec#Arithmetic_operators:
1 2 3 4 5 6 7 8 9 10 11 12 13
| + sum integers, floats, complex values, strings - difference integers, floats, complex values * product integers, floats, complex values / quotient integers, floats, complex values % remainder integers
& bitwise AND integers | bitwise OR integers ^ bitwise XOR integers &^ bit clear (AND NOT) integers
<< left shift integer << integer >= 0 >> right shift integer >> integer >= 0
|
可以通过 play 在线编译看看:https://play.studygolang.com/p/lLMbGE_ajrg