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
- slice — A flexible, growable view over a list of values of one type, written like []string, and Go's everyday list.
- map — A Go collection that stores values by key, such as service name to port number, and looks them up quickly.
- channel — A typed pipe in Go that one goroutine sends values into and another receives them from, safely handing data between them.
- blank identifier — The underscore _ used in place of a variable name to throw away a value you do not need.
Taught in
- Arrays, Slices, and Maps — Concurrent Systems Programming with Go
- Writing Table Driven Tests — Concurrent Systems Programming with Go
- Goroutines and Concurrent Execution — Concurrent Systems Programming with Go
- Channels and Synchronization — Concurrent Systems Programming with Go
- Files and the os Package — Concurrent Systems Programming with Go
- Parsing Arguments with flag — Concurrent Systems Programming with Go
- Project Setup and Configuration — Concurrent Systems Programming with Go
- Executing Concurrent Checks — Concurrent Systems Programming with Go