Stage 1 · Code
Language Depth II
Iterating Collections
Learn the everyday Go patterns for walking through slices, maps, and strings, and understand why byte-by-byte thinking can quietly break the moment real text enters the picture.
Walking Through Slices
Iteration is just repeated attention: look at one item, then the next, then the next. In Go, the most common way to do that is for range. For slices, range can give you both the index and the element value.
If you only need one of those two values, you can ignore the other with _. That keeps the loop honest about what it actually uses.
Iterating Maps Carefully
You can also use range with maps, but there is one rule you must internalize early: map iteration order is intentionally not stable. Go deliberately avoids promising a consistent order so programmers do not accidentally depend on it.
That means a map is for lookup, update, and membership checks. It is not a presentation-order container. If you need a stable report order, collect the keys, sort them, then read the map through that sorted list.
If your program appears to print map entries in the same order a few times, that is luck, not a guarantee. Production code should never rely on map iteration order.
Map Lookups and the Zero-value Trap
Map lookup has its own iteration-adjacent habit you need constantly: the comma-ok form. A plain lookup like m[k] returns the zero value of the map's value type when the key is missing. That is convenient, but it can also lie to you.
Strings Bytes and Runes
Strings need slower, more careful thinking than slices of plain numbers. A Go string is a sequence of bytes. Human text, however, is made of characters, and many characters need more than one byte in UTF-8. That gap is where beginners get tricked.
If you iterate a string byte by byte, you are walking its raw storage. Sometimes that matches human-readable characters, especially for simple ASCII text. But the moment accented letters, emoji, or many non-English scripts appear, one character may occupy multiple bytes. Byte iteration then splits one visible character into fragments.
Tracing Byte vs Rune Iteration
If your goal is to process human text character by character, for _, r := range text is usually the right default. Drop to byte-level iteration only when you truly care about the raw UTF-8 bytes.
Quiz
Why should you not rely on map iteration order in Go?
Practice
Write a function `CountPresent(inventory map[string]int, key string) string`. If `key` exists in the map, return `present:<value>`. If it does not exist, return `missing`. Use the comma-ok idiom.
Summary
for rangeis the standard way to iterate slices, maps, and strings in Go.- Map iteration order is intentionally unspecified, so sort keys first when stable output matters.
- Use the comma-ok idiom on map lookups when zero values and missing keys must be distinguished.
- A Go string stores bytes, but
rangeover a string decodes UTF-8 runes for you. - Byte iteration and rune iteration answer different questions, so choose the one that matches your real goal.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.