Welcome to our lesson on Go's powerful for loop with the range keyword! This lesson is designed for both beginners and intermediate Go programmers, so let's get started!
The for loop is a control structure that allows us to repeat a block of code a specific number of times or until a certain condition is met. The range keyword, on the other hand, is used with for loops to iterate over a sequence such as arrays, slices, maps, or strings.
In this lesson, we will focus on using for loops with range to iterate over slices and strings, which are commonly used in Go programming.
Let's start with an example of iterating over a slice. A slice is a flexible, resizable array in Go.
package main
import "fmt"
func main() {
fruits := []string{"apple", "banana", "cherry", "date"}
for index, fruit := range fruits {
fmt.Println("Index:", index, "Fruit:", fruit)
}
}In the example above, we have a slice named fruits containing four string elements. The for loop with range is used to iterate over each index and corresponding value (fruit) in the slice.
For each iteration, the index variable holds the current index of the element, and the fruit variable holds the current value (fruit name) at that index.
Iterating over strings is similar to iterating over slices. Let's take a look at an example:
package main
import "fmt"
func main() {
greeting := "Hello, World!"
for index, char := range greeting {
fmt.Println("Index:", index, "Character:", char)
}
}In this example, we have a string greeting containing the text "Hello, World!". The for loop with range is used to iterate over each character in the string.
For each iteration, the index variable holds the current index of the character, and the char variable holds the current character at that index.
range with Maps 💡Maps in Go are similar to associative arrays in other programming languages. You can use range with maps to iterate over both keys and values.
Here's an example:
package main
import "fmt"
func main() {
ages := map[string]int{
"Alice": 30,
"Bob": 25,
"Charlie": 40,
}
for key, value := range ages {
fmt.Println("Name:", key, "Age:", value)
}
}In this example, we have a map named ages containing key-value pairs. The for loop with range is used to iterate over both the keys and their corresponding values in the map.
For each iteration, the key variable holds the current key, and the value variable holds the current value associated with that key.
Let's put our newfound knowledge into practice by creating a simple program that calculates the sum of the elements in a given slice.
package main
import "fmt"
func main() {
numbers := []int{1, 2, 3, 4, 5}
sum := 0
for _, number := range numbers {
sum += number
}
fmt.Println("The sum of the numbers is:", sum)
}In this example, we have a slice numbers containing the integers 1 through 5. We initialize a variable sum to 0 and use a for loop with range to iterate over each number in the slice.
For each iteration, we add the current number to the sum variable. After all numbers have been processed, we print the sum.
Question: What is the purpose of the range keyword in Go?
A: To define a range of numbers B: To iterate over a sequence such as arrays, slices, maps, or strings C: To declare a new variable
Correct: B
Explanation: The range keyword in Go is used to iterate over a sequence such as arrays, slices, maps, or strings.
That wraps up our comprehensive lesson on using Go's for loop with the range keyword. By now, you should have a good understanding of how to iterate over slices, strings, and even maps using range. Happy coding! 🚀