Logo

Programming-Idioms

History of Idiom 7 > diff from v32 to v33

Edit summary for version 33 by :
[Go] +DocURL, +DemoURL

Version 32

2015-11-30, 12:37:24

Version 33

2015-12-30, 17:02:40

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 and value at the same time as loop variables
Doc URL
https://golang.org/doc/effective_go.html#for
Demo URL
http://play.golang.org/p/GH_8f06DX5