liveBook · Manning

UTF-8

As explained in Learn Go with Pocket-Sized Projects

UTF-8 is a variable-width Unicode encoding that represents characters as sequences of one to four bytes.

UTF-8 is a variable-width Unicode encoding that represents characters as sequences of one to four bytes. Go uses Unicode natively, while its rune type represents a Unicode code point. section 5.1.2

When handling Go strings, choose the representation that matches the operation:

  • Use the string or []byte representation when measuring or accessing encoded bytes. section 5.1.2
  • Use []rune when counting characters or iterating over Unicode code points. section 5.1.2

Bytes versus runes

fmt.Println(len("Hello, 世界")) prints 13 because the non-Latin characters occupy three bytes each. fmt.Println(len([]rune("Hello, 世界"))) prints 9, counting characters instead.

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains byte-versus-rune lengths, conversions, iteration choices, and input validation without descending into encoding internals.
Examples
Uses "Hello, 世界", multilingual guesses, []byte, and []rune to contrast encoded bytes with characters.
What it assumes
Assumes familiarity with Go strings and len while teaching the distinction between bytes and Unicode code points.
What you can do after
Apply it via Go's []rune conversion to count characters and validate multilingual input correctly.
How it connects
Connects Unicode, rune values, Go strings, byte representations, and character-length validation.

Also explained in

Compare how 11 books explain UTF-8 →

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.