select statement
The select statement lets a Go goroutine wait for multiple channel send or receive operations and executes one case whose communication can proceed. A default case makes the select non-blocking, while a case receiving from ctx.Done() provides a way to respond to context cancellation.
Minimal example
select {
case message := <-messages:
fmt.Println(message)
case <-ctx.Done():
return
default:
return
}