Go has 4 common kinds of literals and a more rare one for complex numbers.

Types of literals

Integer literals

Sequence of numbers.

Base 10 by default, but can be changed with prefixes:

  • 0b for binary (base 2)
  • 0o for octal (base 8). Can also be represented with 0 (no letter), but it’s not recommended to use it as it is very confusing.
  • 0x for hexadecimal (base 16).

Underscores _ can be used in the middle of integer literals that have no effect on the literal value to improve readability. This allows to group, for example, by thousands (1_234) in base 10 or beak up at 1-, 2- or 4-byte boundaries for other bases. Can’t be used at the start or beginning of the literal. Can’t be placed next to each other (__).

Floating-point literals

Has a decimal point to indicate a fractional portion of the value

Can have an exponent with the letter e with a positive/negative number, e.g. 6.03e23.

Hexadecimal literals can be written with 0x prefix and the letter p for exponent, e.g. 0x12.34p5 (equals 582.5 in base 10).

Underscores can be used as well.

Imaginary literals for complex numbers

Same as floating-point literals but have an i suffix: 36.62i

Rune literals

Characters in single quotes.

  • 'a' - Unicode character
  • '\141' - 8-bit octal number
  • '\x61' - 8-bit hexadecimal number
  • '\u0061' - 16-bit hexadecimal number
  • '\U0000006' - 32-bit Unicode number
  • and some backslash-escaped rune literals like new-line '\n', tab '\t', single quote '\'', backslash '\\'

String literals

Interpreted string literals are indicated with double quotes. They are called “interpreted” because they interpret rune literals inside, e.g. "Greetings and\n\"Salutations\""

Raw string literals are indicated with backquotes/backticks. They can contain any character except a backquote. They don’t have any escape characters, and all characters are included as is, including newlines. So it’s possible to write multiline strings like that:

`Greetings and
"Salutations"`

Literals are untyped

Go uses the default type for a literal; if there’s nothing in the expression that makes clear what the type of the literal is, the literal defaults to a type.

var x float64 = 10
var y float64 = 200.3 * 5