liveBook · Manning

Rune

As explained in Go in Action, Second Edition

A rune is Go's alias for int32 that represents a Unicode code point independently of its encoded representation.

A rune is Go’s alias for int32, used when an integer represents a Unicode character rather than arbitrary numeric data. It represents the character value independently of its encoded byte length. section 3.1 section 3.8.6

Use runes when character-oriented behavior matters:

  • Range over a string to receive one rune per character while retaining the byte index where that character begins. section 3.8.5
  • Convert a string to []rune when indexes should represent character positions rather than byte offsets. section 3.8.7
  • Pass runes to unicode and unicode/utf8 functions to classify characters, change case, or inspect encoded length. section 3.8.6

Iterate Unicode characters

unicodeCharStr := "地鼠" for i, r := range unicodeCharStr { fmt.Printf("%d:%s ", i, string(r)) } // Output: 0:地 3:鼠 characters := []rune(unicodeCharStr) // The rune slice indexes the characters as 0 and 1. section 3.8.5 section 3.8.7

Inspect a rune

for _, r := range "À" { fmt.Println(utf8.RuneLen(r)) if unicode.IsLetter(r) && unicode.IsUpper(r) { fmt.Println(string(unicode.ToLower(r))) } } // Output includes: 2 and à section 3.8.6

Table 3.1 Predeclared integer types

Type name

Description

Value vange

int

Signed integers

32 or 64 bits, depending on your architecture

int8

Signed 8-bit integers

-128 to 127

int16

Signed 16-bit integers

-32,768 to 32,767

int32

Signed 32-bit integers

-2,147,483,648 to 2,147,483,647

int64

Signed 64-bit integers

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

uint

Unsigned integers

32 or 64 bits, depending on your architecture

uint8

Unsigned 8-bit integers

0 to 255

uint16

Unsigned 16-bit integers

0 to 65,535

uint32

Unsigned 32-bit integers

0 to 4,294,967,295

uint64

Unsigned 64-bit integers

0 to 18,446,744,073,709,551,615

byte

An alias for uint8

rune

An alias for int32

Compare rune with Go’s other predeclared integer aliases and fixed-width integer types. section 3.1

How Go in Action, Second Edition covers it

How deep it goes
Defines the int32 alias, demonstrates range and []rune conversion, and applies Unicode packages without detailing their full APIs.
Examples
Demonstrates 地鼠 iteration, []rune indexing, and À classification, case conversion, and encoded-length inspection.
What it assumes
Assumes familiarity with Go integer types and strings while teaching Unicode-oriented character handling from the alias definition onward.
What you can do after
Apply it via range loops and Unicode packages to process, classify, compare, and inspect characters.
How it connects
Connects runes with strings, Unicode, UTF-8, range loops, character indexing, and Unicode classification.

Also explained in

Compare how 4 books explain rune →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.