iterate over array golanf code example
Example 1: golang loop through array
arr := []int{1, 2, 3, 4, 5}
//using range
for index, ele := range arr { // you can escape index by _ keyword
fmt.Println(index, ele)
}
Example 2: iterating through string golang
package main
import (
"fmt"
"strings"
)
func main() {
words := strings.Fields("This, that, and the other.")
for i, word := range words {
fmt.Println(i, " => ", word)
}
}