closing a channel
Calling close on a channel to announce that no more values will be sent, which lets receivers stop ranging over it.
In more detail
Only the sender should close, and only once: sending on or closing an already closed channel panics. Receivers can keep reading the remaining buffered values, then get the zero value with ok set to false. A for range loop over a channel ends when it is closed.
Example
wait.Wait()
close(results)Related terms
- channel — A typed pipe in Go that one goroutine sends values into and another receives them from, safely handing data between them.
- range loop — A Go for loop with the range keyword that visits each element of a slice, map, string or channel in turn.
- panic — A Go run-time failure that stops normal execution, runs deferred functions and, unless recovered, crashes the program with a stack trace.
- sync.WaitGroup — A counter from Go's sync package that lets one goroutine wait until a group of other goroutines have all finished.
Taught in
- Channels and Synchronization — Concurrent Systems Programming with Go