× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

In golang, how to iterate two arrays using one `range`

Simone Carletti
Mar 21, 2015
<p>You cannot, but if they are the same length you can use the index from <code>range</code>.</p> <pre><code>package main import ( "fmt" ) func main() { r1 := []int{1, 2, 3} r2 := []int{11, 21, 31} for i, _ := range r1 { fmt.Println(r1[i]) fmt.Println(r2[i]) } } </code></pre> <p>It returns</p> <pre><code>1 11 2 21 3 31 </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/28047311/In%20golang,%20how%20to%20iterate%20two%20arrays%20using%20one%20%60range%60/28047625">Stack Overflow</a>.</p>
comments powered by Disqus