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 fact, there are many ways to write zero in base 8 in Go, including 0, 00 and 000. If you prefer hexadecimal notation, you also have a smörgåsbord of options: such as 0x0, 0x00 and 0x000 (as well as 0X0, 0X00 and 0X000). 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 1.

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: