Golang range: Slice, String and Map

Use range expressions in for-loops on slices, strings and maps.

Range. A slice has indexes 0, 1, 2. With a range expression in a for-loop we iterate over those indexes. We can also access element values.Built-Ins

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.

Description: The first for-loop accesses only the indexes. The second accesses indexes and element values themselves.

Golang program that uses range on 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) } } Output 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.Strings

Note: In Go we refer to chars as runes. Rune has some technical meaning, but mostly is a fancy word for char.

Golang program that uses range on string 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)) } } Output 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

Then: We use a range over the keys of the map. In Go, maps return their keys in a somewhat random (unpredictable) way.

Key, value: 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.

Golang program that uses range on 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) } } Output 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.For

Wide open ranges. 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
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to