Literals

A detailed guide to integer, floating-point, imaginary, rune, and string literals in Go, covering syntax, base prefixes, underscores, escape sequences.

A literal is a value written directly in source code, a number like 42, a string like "hello", or a character like 'A'. The compiler reads the literal and turns it into a value your program can use without any computation. Every programming language has literals; Go's are precise about what is allowed, and that precision prevents entire categories of bugs.

This page covers the five literal kinds in Go: integers, floating-point numbers, imaginary numbers, runes, and strings. Each child page explains the syntax, the rules, and the mistakes beginners make most often.

Integer Literals

An integer literal is a whole number written directly in code, like 42 or 0b1010. This page covers the four number bases - decimal, binary, octal, and hexadecimal - underscore separators for readability, and how untyped integer constants take their type from context.

Floating-Point Literals

Floating-point literals represent numbers with fractional parts. This page covers decimal and hexadecimal floating-point forms, the mandatory p exponent for hex floats, digit separators, and the difference between compile-time constant precision and runtime float64 values.

Imaginary Literals

Go has built-in support for complex numbers. An imaginary literal is an integer or floating-point literal followed by the letter i. This page covers the syntax, the octal backward-compatibility rule for leading zeros, underscore rules, and how imaginary literals combine with a real part to form complex constants.

Rune Literals

A rune literal represents a single Unicode code point, written between single quotes, like 'a'. This page covers the one-code-point rule, escape sequences, the difference between a rune and a byte, and the mistakes that come from treating a rune like a one-character string.

String Literals

String literals are sequences of characters enclosed in either double quotes " (interpreted) or backticks ` (raw). This page covers both forms, the full escape list, carriage-return stripping in raw strings, and why a string literal is a constant you cannot take the address of.

Summary

Integer, floating-point, imaginary, rune, and string literals share one goal: they let you express constant values directly in code, with rules that make the code unambiguous to both the compiler and other developers. The base prefixes for integers, the mandatory p exponent for hex floats, the i suffix for imaginary numbers, the single-quote-for-runes rule, and the two string forms each remove guesswork - you know exactly what value you are creating just by looking at the syntax.

The most important skill to build now is reading a literal and immediately recognising its type and possible pitfalls. When you see 0123, the leading zero means octal; when you see 0x1.5, check for the p exponent; when you see 'a', remember it is a number. With those reflexes, the rest of Go's type system and constant rules will feel predictable rather than surprising.

Integer Literals

How to write integer constants in Go using decimal, binary, octal, and hexadecimal forms, digit separators for readability.

Floating-Point Literals

A complete guide to Go's floating-point literals, decimal and hexadecimal forms, exponent notation, digit separators, compile-time evaluation.

Imaginary Literals

How Go represents imaginary numbers as literals, their syntax rules, and how to use them to form complex constants

Rune Literals

Understand rune literals in Go - single-quoted Unicode code point constants, supported escape sequences.

String Literals

A complete guide to string literals in Go, covering interpreted and raw forms, escape sequences, and common mistakes