range
range gives index and value for slices, keys and values for maps and runes for strings.
Example
package main
import "fmt"
func main() {
a := []string{"x", "y"}
for i, v := range a {
fmt.Println(i, v)
}
for i := range 3 {
fmt.Print(i, " ")
}
fmt.Println()
}Result
0 x 1 y 0 1 2
Exercise
Ignore the index.
for , v := range a
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.