Numbers that start with zero
What’s up with the counting in this example?
const ( Century = 100 Decade = 010 Year = 001 ) // The world's oldest person, Emma Morano, lived for a century, // two decades and two years. fmt.Println("She was", Century+2*Decade+2*Year, "years old.")
She was 118 years old.
010 is a number in base 8, therefore it means 8, not 10.
Integer literals in Go are specified in octal, decimal or hexadecimal.
The number 16 can be written as
||10||Never starts with
This bitwise operators cheat sheet covers all bitwise operators and functions in Go.
Zero knowledge (trivia)
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.
In fact, Go doesn’t have any negative decimal literals either:
-1 is the unary negation operator
followed by the decimal literal
Share this page: