Skip to main content
Module 4 · Lesson 4 of 519/50 in the course~12 min
Module lessons (4/5)

Strings, byte and rune

Lesson preview5 hands-on exercises readyCourse Go · Module 4 — Collections

The strings are byte UTF-8; range produces rune; len() counts byte not characters.

What you'll learn in this lesson

  • Distinguish between number of bytes (len(s)) and number of Unicode characters (utf8.RuneCountInString(s)).
  • Decode multi-byte UTF-8 characters using for range or converting to []rune.
  • Edit and concatenate text efficiently while respecting the immutability of string with []byte and strings.Builder.

Overview

In Go a string is a read-only (immutable) slice of bytes typically encoded in UTF-8. A character like 'a' takes up 1 byte, 'à' takes up 2 bytes, and an emoji '🚀' takes up 4 bytes.

You can't assign s[0] = 'C' directly: you have to convert to []byte (for ASCII) or []rune (for generic Unicode), edit and convert back to string:

Sections covered in this lesson

  1. 1Bytes vs rune (int32)
  2. 2Immutability and controlled changes
  3. 3Efficient loop concatenation: strings.Builder
  4. 4Truncate safely on Unicode boundaries
  5. 5Guided practice
  6. 6Check for understanding
Pro Lesson · Module 4 — Collections

Unlock "Strings, byte and rune" and the full Course Go curriculum

The strings are byte UTF-8; `range` produces rune; `len()` counts byte not characters.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course Go plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.