Zero knowledge (besserwisser alert)
0 isn’t a decimal number in Go – it’s an octal literal.
An integer literal in Go (as well as in C and Java) is
- in base 16 if it starts with
- in base 8 if it starts with
- otherwise it's a decimal number.
In fact, there are many ways to write zero in base 8 in Go, including
If you prefer hexadecimal notation, you also have a smörgåsbord of options: such as
(as well as
However, there is no decimal zero integer literal in Go.
Note that Go doesn’t have any negative decimal literals either:
-1 is the unary negation operator
followed by the decimal literal
Why should I care?
This is mostly useless knowledge, unless perhaps you’re writing a Go compiler. But it’s good to be aware of the octal integer literals. If your write a number with leading zeros by mistake, you could be in for a nasty surprise.
Share this page: