Iterating Collections with range
Using range over slices, maps and strings
Iterating Collections with range is a free Go Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is range?
range is a special form of the for loop that iterates over arrays, slices, maps, strings, and channels. It returns an index (or key) and a value on each iteration.
range Over a Slice
Returns the index and a copy of the element:
package main
import "fmt"
func main() {
fruits := []string{"apple", "banana", "cherry"}
for i, fruit := range fruits {
fmt.Printf("%d: %s\n", i, fruit)
}
}range Over an Array
Works identically to slices. Array elements are copied per iteration:
package main
import "fmt"
func main() {
nums := [3]int{10, 20, 30}
for _, v := range nums {
fmt.Println(v)
}
}range Over a Map
Returns the key and value in random order:
package main
import "fmt"
func main() {
capitals := map[string]string{
"France": "Paris",
"Japan": "Tokyo",
}
for country, city := range capitals {
fmt.Printf("%s -> %s\n", country, city)
}
}range Over a String
Iterates over Unicode code points (rune), not bytes. The index is the byte position of the rune start:
package main
import "fmt"
func main() {
for i, r := range "héllo" {
fmt.Printf("%d: %c\n", i, r)
}
}Discarding the Index
Use _ to ignore the index when you only need values:
package main
import "fmt"
func main() {
total := 0
for _, v := range []int{1, 2, 3, 4, 5} {
total += v
}
fmt.Println(total) // 15
}Index-Only range
Omitting the value gives just the index. Useful for in-place mutation:
package main
import "fmt"
func main() {
s := []int{1, 2, 3}
for i := range s {
s[i] *= 2
}
fmt.Println(s) // [2 4 6]
}range Over a Channel
range reads from a channel until it is closed:
package main
import "fmt"
func main() {
ch := make(chan int, 3)
ch <- 1; ch <- 2; ch <- 3
close(ch)
for v := range ch {
fmt.Println(v)
}
}The Loop Variable Trap
The range variable is reused each iteration — capturing its address in a goroutine or closure is a classic bug:
package main
import "fmt"
func main() {
nums := []int{1, 2, 3}
funcs := make([]func(), 3)
for i, v := range nums {
v := v // shadow with a copy
funcs[i] = func() { fmt.Println(v) }
}
for _, f := range funcs { f() } // 1 2 3
}Modifying Slice Elements
The range value is a copy. To modify, use the index:
package main
import "fmt"
func main() {
s := []int{10, 20, 30}
for i := range s {
s[i]++
}
fmt.Println(s) // [11 21 31]
}Quick Check
When ranging over a string, what type is the value variable?
Recap: range
Key takeaways:
rangeworks on slices, arrays, maps, strings, and channels- Returns (index, value) — either can be discarded with
_ - Range values are copies — mutate via index
- Strings yield runes, not bytes
- Map iteration order is random
Practice Prompt
Use range to compute the sum of all values in a map[string]int and print both each entry and the total.
Frequently asked questions
Is the “Iterating Collections with range” lesson free?
Yes — the full text of “Iterating Collections with range” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Iterating Collections with range”?
Using range over slices, maps and strings You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Iterating Collections with range” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Arrays: Fixed-Length Collections
- Slices: Dynamic Lists
- Maps: Key-Value Stores
- Iterating Collections with range