Range over channels
Context
Section titled “Context”The for range loop can iterate over values sent on a channel until the channel is closed. This is a clean way to receive all values.
Example
Section titled “Example”Send multiple values and use range to receive them.
Code example
Section titled “Code example”package main
import "fmt"
func main() { ch := make(chan int, 3) ch <- 10 ch <- 20 ch <- 30 close(ch)
for v := range ch { fmt.Println(v) }}Output
Section titled “Output”102030