By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
range visits every item of a collection in turn, so you do not manage an index yourself. Use it when you want to look at every item and do not care how many there are. It is the loop you will reach for most often once collections arrive in section 5.
A slice is an ordered list of values of one type. Section 5 studies slices properly; for now, read []string{"coffee", "bus", "lunch"} as a list literal holding three strings. range over that slice gives two values each pass: the index and the value.
package main
import "fmt"
func main() {
expenses := []string{"coffee", "bus", "lunch"}
for i, item := range expenses {
fmt.Println(i, item)
}
}The index starts at 0, so the last item sits at len(expenses) - 1. range stops after the last item, which is why you never write a condition for it. The diagram shows both values that range hands you, and what a string gives instead.
The value range hands you is a copy of the item. Changing it inside the loop does not change the slice. To change the slice, use the index: items[i] = newValue. This detail catches almost every beginner once, and reading it here saves you that afternoon.
When you do not need one of the values, use the blank identifier _. for _, amount := range cents ignores the index and gives you values. for i := range cents gives only indices. for range cents runs once per item and hands you nothing at all. The underscore tells the compiler that you know the value exists and are deliberately dropping it, so the declared-and-not-used rule stays happy.
package main
import "fmt"
func main() {
cents := []int{450, 200, 875}
total := 0
for _, amount := range cents {
total += amount
}
fmt.Println("total:", total)
}The total starts at 0 before the loop and grows by each amount. A variable declared outside the loop and updated inside it is called an accumulator, and you will use the pattern constantly.
A string ranges differently. For each character, range gives the byte index where that character starts and the rune, which is the character stored as a number. For plain ASCII the byte index and the character count agree. For accented letters they do not.
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
word := "café"
for i, r := range word {
fmt.Printf("index %d, rune %c\n", i, r)
}
fmt.Println("runes:", utf8.RuneCountInString(word))
fmt.Println("bytes:", len(word))
}Read the output: the accented e is one rune but two bytes, so the index jumps from 2 to 4 and the loop visits four characters while len reports five bytes. %c prints the rune as a character; %d would print its number.
Build a slice of four prices in cents, then use range to print each one and a running total. Run it, and check the total by adding the four numbers yourself. Then add a second loop that prints only the prices over 500 cents, with an if inside the loop. Finally, range over the text "Go!" and print each index and character, then do the same with your own name and compare how the indices move.
Key takeaways
range over a slice gives the index and the value; the index starts at 0.
The value is a copy, so use the index when you need to change the slice.
_ drops a value you do not need, and for range items visits without values.
Ranging over a string gives a byte index and a rune.
len counts bytes while range visits characters.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.