range loop

A Go for loop with the range keyword that visits each element of a slice, map, string or channel in turn.

In more detail

Over a slice it gives an index and a value; over a map, a key and a value in no fixed order. Over a channel it receives values until the channel is closed. Use _ for the part you do not need.

Example

for i, h := range hosts {
	fmt.Printf("%d %s\n", i+1, h)
}

Related terms

Taught in

References