package main
const X = '\x61' // 'a'
const Y = 0x62
const A = Y - X // 1
const B int64 = 1
var n = 32
func main() {
if A == B {
println(A << n >> n, B << n >> n)
}
}
Choices:
Answer: 0 1
Run it on Go play.
Key points:
X
is a rune constant (in other words, its default type is rune
, a.k.a int32
, a 32-bit integer type).
Untyped Y
is an int constant (in other words, its default type is int
, a 64-bit integer type on 64-bit OSes).
A << n
overflows, so it is evaluated as 0;
on the other hand, the expression B << n
doesn't overflow.
The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.
If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account @zigo_101.