Range ClausesSpec: http://golang.org/doc/go_spec.html#For_statements SummaryA range clause provides a way to iterate over a array, slice, string, map, or channel. Examplefor k, v := range myMap {
log.Printf("key=%v, value=%v", k, v)
}
for v := range myChannel {
log.Printf("value=%v", v)
}ReferenceIf only one value is used on the left of a range expression, it is the 1st value in this table.
GotchasWhen iterating over a slice or map of values, one might try this: items := make([]map[int]int, 10)
for _, item := range items {
item = make(map[int]int, 1) // Oops! item is only a copy of the slice element.
item[1] = 2 // This 'item' will be lost on the next iteration.
}The make and assignment look like they might work, but the value property of range (stored here as item) is a copy of the value from items, not a pointer to the value in items. The following will work: items := make([]map[int]int, 10)
for i := range items {
items[i] = make(map[int]int, 1)
items[i][1] = 2
}
| |||||||||||||||||