When programming in Go, you often encounter collections such as arrays, slices, and maps. A common task involves iterating through these collections to access or manipulate each element. Go provides a convenient keyword, range, specifically for this purpose. In this article, we’ll walk through real-world examples illustrating how range can be used to effectively iterate through different types of collections in Go.
Understanding range
The range keyword in Go iterates over various data structures such as arrays, slices, maps, and strings, conducting operations on each element. During iteration, range can provide index and value for arrays and slices, key and value for maps, and index and rune for strings.
Basic Example: Iterating Over Arrays
package main
import "fmt"
func main() {
numbers := [3]int{10, 20, 30}
for index, value := range numbers {
fmt.Printf("Index: %d, Value: %d\n", index, value)
}
}
This basic example demonstrates how range is employed to loop over an array called numbers. Here, both the index and the value of each element are printed.
Intermediate Example: Iterating Over Slices
package main
import "fmt"
func main() {
fruits := []string{"Apple", "Banana", "Cherry"}
for index, fruit := range fruits {
fmt.Printf("Index: %d, Fruit: %s\n", index, fruit)
}
}
Slices in Go provide a more powerful and flexible way to manage sequences of data compared to arrays. This intermediate example utilizes a slice of strings representing fruits, demonstrating how to utilize range to access each element.
Advanced Example: Iterating Over Maps
package main
import "fmt"
func main() {
countryCapitals := map[string]string{
"France": "Paris",
"Japan": "Tokyo",
"Australia": "Canberra",
}
for country, capital := range countryCapitals {
fmt.Printf("The capital of %s is %s\n", country, capital)
}
}
Maps in Go are key-value data structures, offering efficient lookups and being highly versatile for tasks like aggregating data. In this advanced example, the range keyword is used to iterate through a map where the keys are country names and the values are their capitals. This allows both keys and values to be accessed and printed.
Advanced Example: Iterating Over Strings
package main
import "fmt"
func main() {
str := "Hello, 世界"
for index, runeValue := range str {
fmt.Printf("Index: %d, Rune: %c\n", index, runeValue)
}
}
In Go, strings are sequences of bytes that can contain characters from UTF-8 character set. This advanced example shows how to iterate over a string containing both ASCII and non-ASCII characters. The index and the rune (which represents each Unicode character) are printed during each iteration.
Conclusion
The range keyword in Go is a powerful tool for accessing and manipulating collections like arrays, slices, maps, and strings. As we’ve seen, whether you’re working with simple arrays or complex data structures like maps, range delivers a clear and concise syntax for iterating, reducing the effort needed for common tasks while maintaining readability and efficiency.