Recall & Review
beginner
What is the basic way to loop over an array in Go?
Use the
for loop with range to get index and value for each element in the array.Click to reveal answer
beginner
What does the
range keyword return when used with an array?It returns two values: the current index and the value at that index in the array.
Click to reveal answer
beginner
How can you ignore the index when iterating over an array in Go?
Use an underscore
_ in place of the index variable to ignore it, like for _, value := range arr.Click to reveal answer
intermediate
Can you modify array elements while iterating with
range in Go?No, the value returned by
range is a copy. To modify elements, use the index to access the array directly.Click to reveal answer
beginner
Write a simple Go code snippet to print all elements of an array of integers.
```go
package main
import "fmt"
func main() {
arr := [3]int{10, 20, 30}
for i, v := range arr {
fmt.Printf("Index %d: Value %d\n", i, v)
}
}
```Click to reveal answer
What does the
range keyword return when used on an array in Go?✗ Incorrect
range returns both the index and the value for each element when iterating over an array.
How do you ignore the index when using
range to iterate over an array?✗ Incorrect
Using _ tells Go to ignore the index value.
Can you change the elements of an array by modifying the value variable inside a
range loop?✗ Incorrect
The value variable is a copy, so modifying it does not change the original array element.
Which of these is the correct syntax to iterate over an array
arr in Go?✗ Incorrect
Go uses for i, v := range arr to iterate over arrays.
What happens if you omit the index variable in a
range loop like for v := range arr?✗ Incorrect
You must specify both index and value or use _ to ignore one. Omitting the index variable causes a compile error.
Explain how to use a
for range loop to iterate over an array in Go and print each element.Think about how you get both index and value from the array.
You got /4 concepts.
Describe why modifying the value variable inside a
range loop does not change the original array elements.Consider how Go handles values when iterating.
You got /3 concepts.