Home
Go
range Keyword
This page was last reviewed on Sep 18, 2023.
Dot Net Perls
Range. Consider a Golang program that has a slice with indexes 0, 1, 2. With a range expression in a for-loop we iterate over those indexes.
for
Shows a slice
One or two parts. A range can be used on a slice, string or map. The first variable from a range is the index. The second is the element itself (like a slice element or a rune).
Slice example. We have a slice of string elements here—a string slice. This slice has 3 elements. We use range in a for-loop on the slice.
Info The first for-loop accesses only the indexes. The second accesses indexes and element values themselves.
Slice
Shows a slice
package main import "fmt" func main() { colors := []string{"blue", "green", "red"} // Use slice range with one value. // ... This loops over the indexes of the slice. for i := range colors { fmt.Println(i) } // With two values, we get the element value at that index. for i, element := range colors { fmt.Println(i, element) } }
0 1 2 0 blue 1 green 2 red
String. Here is a string range example. We can use 1 or 2 variables in the range clause. The first is the rune index, and the second is the rune value itself.
Note In Go we refer to chars as runes. Rune has some technical meaning, but mostly is a fancy word for char.
package main import "fmt" func main() { // This is a string. name := "golang" // Use range on string. // ... This accesses only the indexes of the string. for i := range name { fmt.Println(i) } // Use range with two variables on string. // ... This is an index and a rune (char of the string). for i, element := range name { // Convert element to string to display it. fmt.Println(i, string(element)) } }
0 1 2 3 4 5 0 g 1 o 2 l 3 a 4 n 5 g
Map. Here is a map. We use int keys and string values. We map the English words for some numbers—this could be useful in a real program.
map
Then We use a range over the keys of the map. In Go, maps return their keys in a somewhat random (unpredictable) way.
Info We can access both the key and the value at each pair of the map. This is an efficient way to loop over a map.
package main import "fmt" func main() { // An example map. words := map[int]string{ 0: "zero", 1: "one", 2: "two", 3: "three", } // Use range on map to loop over keys. for key := range words { fmt.Println(key) } // Range on map can access both keys and values. for key, value := range words { fmt.Println(key, value) } }
2 3 0 1 3 three 0 zero 1 one 2 two
For. A for-loop can be used with a range expression. We can loop over values with a start, an end, and an increment statement. In Go we use the for-loop.
Summary. In Go we use a range expression to enumerate arrays, slices, maps, strings and channels. For most uses, a range can be used with one or two variables.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 18, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.