liveBook · Manning

Rune

As explained in Learn Go with Pocket-Sized Projects

A rune is Go's value type representing a single Unicode code point.

A rune is Go's value type for one Unicode code point. Write rune literals with single quotes, such as '學'; use double quotes for strings and backticks for raw strings. section 2.3.2

Converting a string to []rune lets Go iterate through its Unicode code points, but a rune does not necessarily represent a user-perceived character. section 5.4

Keep these distinctions in mind:

  • Use single quotes for one rune, double quotes for ordinary string literals, and backticks for raw string literals. section 2.3.2
  • Treat combining marks carefully: []rune("नमस्त") produces separate code points rather than combining related symbols into one unit. section 5.4
  • Use golang.org/x/text/unicode/norm when Unicode normalization and character combination exceed native rune handling. section 5.4

Inspecting runes

The loop `for _, r := range []rune(s)` converts a string to runes and prints each code point separately, making combining marks visible. section 5.4

Listing 5.40 Understanding the नमस्ते case
func main() {
    s := "नमस्त"
    for _, r := range []rune(s) {
        fmt.Print(string(r) + " ")
    }
}

Inspect the runnable Devanagari example that prints each rune in a string. section 5.4

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Defines rune literals and demonstrates iteration, then stops short of treating grapheme-like character units as native runes.
Examples
Uses '學', multilingual greetings, and the नमस्त string to contrast rune literals, strings, and combining marks.
What it assumes
Assumes familiarity with Go syntax and strings while distinguishing three Go literal forms.
What you can do after
Apply rune literals and []rune iteration, or choose Unicode normalization when code points do not match desired character units.
How it connects
Connects runes with Unicode code points, strings, combining marks, and Unicode normalization.

Also explained in

Compare how 4 books explain rune →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.