Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A rune in Go is a built-in type alias for int32 that represents a single Unicode code point. It serves as the language’s standard mechanism for isolating and representing individual characters, distinguishing them from raw bytes or standard numeric integers.

Technical Specifications

  • Underlying Type: int32
  • Memory Footprint: 4 bytes (32 bits)
  • Zero Value: 0 (which corresponds to the null character '\x00')
  • Encoding: Represents a specific Unicode code point, independent of its UTF-8 byte-length representation.

Syntax and Literals

Rune literals are expressed by enclosing one or more characters in single quotes (' '). Go supports multiple ways to define a rune literal, including raw characters, octal/hexadecimal escapes, and Unicode escapes.
// Implicit typing (inferred as rune/int32)
var a = 'A'

// Explicit typing
var b rune = 'Ω'

// Escape sequences
var newline rune = '\n'

// Unicode code point escapes
var c rune = '\u03A9'       // 16-bit Unicode escape (Omega)
var d rune = '\U000003A9'   // 32-bit Unicode escape (Omega)

Memory and Type Mechanics

Because Go strings are read-only slices of bytes (uint8), accessing a string via a standard index returns a single byte, not a complete character. A rune resolves this by allocating exactly 32 bits, which is sufficient to hold any valid Unicode code point (which requires up to 21 bits).
// A string containing a 4-byte Unicode character
str := "Go🚀"

// Standard indexing yields a single byte (uint8)
// str[2] returns the first byte of the 🚀 character
fmt.Printf("%T\n", str[2]) // Output: uint8

// Converting to a rune slice yields complete Unicode code points (int32)
runes := []rune(str)
fmt.Printf("%T\n", runes[2]) // Output: int32 (rune)

Iteration Behavior

When iterating over a string using a for...range loop, Go implicitly decodes the underlying UTF-8 byte sequence. Instead of yielding individual bytes, the loop yields rune values alongside their starting byte index.
str := "café"

for index, char := range str {
    // 'char' is strictly of type rune (int32)
    // Because 'é' occupies 2 bytes, the index advances accordingly
    fmt.Printf("Type: %T, Value: %U\n", char, char)
}
Master Go with Deep Grasping Methodology!Learn More