In Go (golang), how to iterate two arrays, slices, or maps using one `range`

You cannot, but if they are the same length you can use the index from range.

package main

import (
    "fmt"
)

func main() {
    r1 := []int{1, 2, 3}
    r2 := []int{11, 21, 31}

    if len(r1) == len(r2) {
        for i := range r1 {
            fmt.Println(r1[i])
            fmt.Println(r2[i])
        }
    }
}

It returns

1
11
2
21
3
31

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)