Coding/Learn/go/go strings runes utf8
Go Foundational Course
01.Introduction, Toolchain & Package main
02.Variables, Zero Values, Constants & iota
03.Control Flow: If with Init Statements & Switch
04.The Unified For Loop & Range Iteration
05.Functions, Multiple Return Values & Named Returns
06.Arrays vs Slices: Dynamic Memory & Headers
07.Maps: Key-Value Hash Tables & Safe Access
08.Pointers & Value vs Pointer Semantics
09.Structs, Receiver Methods & Composition
10.Implicit Interfaces & Duck Typing with Typesafety
11.Explicit Error Handling, Wrapping & Inspection
12.Defer, Panic & Recover: Resource Cleanup
13.Strings, Runes & UTF-8 Encoding Internals
14.Goroutines & The M:N Work-Stealing Scheduler
15.Channels: CSP Concurrency & Pipelines
16.Select Statement, Non-Blocking IO & Timeouts
17.Mutexes, RWMutex & Atomic Primitives
18.Context Propagation, Cancellation & Deadlines
19.JSON Serialization, Tags & net/http Routing
20.Capstone: High-Performance Concurrent Microservice Engine
Course Certificate0%

Complete all lessons to earn

LearnStrings, Runes & UTF-8 Encoding Internals
13 / 20 0%
Lesson 13 of 20
12 mins read

Strings, Runes & UTF-8 Encoding Internals

PreviousNext Lesson
In Plain English

Strings in Go are read-only slices of UTF-8 encoded bytes. Master the difference between byte length (len) and character count (utf8.RuneCountInString), rune literals, and string formatting.

The Mental Model

"A string is a train of cargo containers (bytes). In ASCII, each passenger fits in 1 container. In Unicode UTF-8, an international passenger (rune) occupies 2 to 4 linked containers."

Deep Dive: How It Works

Strings as Byte Slices: string is an immutable slice of bytes with length.

Rune as Unicode Codepoint: rune is an alias for int32 representing a single Unicode character.

UTF-8 Iteration: for i, r := range s decodes UTF-8 runes automatically, advancing byte indices by rune width.

Syntax Blueprint

s := "Go 禅"
byteCount := len(s)
runeCount := utf8.RuneCountInString(s)
for i, r := range s { ... }

Measure byte length with len(), Unicode count with utf8.RuneCountInString, and iterate runes with range.

PointCore Rules to Remember

Point star
Immutable Bytes: Strings are immutable byte slices under the hood.
Point star
Byte vs Rune Length: len(s) returns byte count, while utf8.RuneCountInString returns Unicode character count.
Point star
Rune Alias: rune is int32 representing a Unicode code point.

Common Beginner Traps & How to Fix Them

Mistake:Using s[0:3] on multi-byte international text expecting 3 characters.

Why it happens: String slicing operates on raw bytes, which can split a multi-byte UTF-8 character in half.

How to fix: Convert to []rune first: runes := []rune(s); sub := string(runes[0:3]).

Pro Tip: Use strings.Builder for high-performance string concatenations inside loops to avoid intermediate heap allocations.

Live Interactive Example

Hit Run Code to see it live
Byte vs Rune Inspection of Multilingual Text
Go 1.22 / Runtime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Output Console
Click "Run Code" to view the rendered output.
How it works: Index jumped from 3 to 6 because the Zen character occupies 3 UTF-8 bytes.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Count Unique Vowels via Runes

  • PointWrite a function countVowels(s string) int.
  • PointIterate through the string runes, counting occurrences of 'a', 'e', 'i', 'o', 'u' (case-insensitive).
  • PointIn main(), test with "Golang Runtime" and print "Vowels: %d".
1
2
3
4
5
6
7
8
9
10
11
12
13
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates