Logo

Programming-Idioms

History of Idiom 7 > diff from v50 to v51

Edit summary for version 51 by programming-idioms.org:
[Go] Better comment

Version 50

2016-06-05, 15:06:23

Version 51

2016-06-05, 16:53:12

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Imports
import "fmt"
Imports
import "fmt"
Code
for i, x := range items {
    fmt.Printf("Item %d = %v \n", i, x)
}
Code
for i, x := range items {
    fmt.Printf("Item %d = %v \n", i, x)
}
Comments bubble
The range clause gives you index and value at the same time as loop variables
Comments bubble
The range clause gives you index i and value x at the same time as loop variables
Doc URL
https://golang.org/doc/effective_go.html#for
Doc URL
https://golang.org/doc/effective_go.html#for
Demo URL
http://play.golang.org/p/GH_8f06DX5
Demo URL
http://play.golang.org/p/GH_8f06DX5