channel
A typed pipe in Go that one goroutine sends values into and another receives them from, safely handing data between them.
In more detail
Create one with make(chan string); send with ch <- v and receive with v := <-ch. On an unbuffered channel a send waits until a receiver is ready, so the handoff also synchronises the two goroutines. Passing a value through a channel is a clear way to transfer ownership of it.
Example
values := make(chan string)
go func() { values <- "ready" }()
value := <-valuesRelated terms
- goroutine — A function running concurrently with the rest of a Go program, started by putting the go keyword before a call.
- buffered channel — A channel created with a capacity, such as make(chan int, 2), that can hold that many values before a send has to wait.
- closing a channel — Calling close on a channel to announce that no more values will be sent, which lets receivers stop ranging over it.
- select statement — A Go statement that waits on several channel operations at once and runs the first one that is ready.
- make (built-in) — The built-in Go function that creates a ready-to-use map, slice or channel, such as make(map[string]bool) or make(chan string).
Taught in
- Goroutines and Concurrent Execution — Concurrent Systems Programming with Go
- Channels and Synchronization — Concurrent Systems Programming with Go
- The select Statement — Concurrent Systems Programming with Go
- The context Package — Concurrent Systems Programming with Go
- Context Cancellation and Timeouts — Concurrent Systems Programming with Go
- Graceful Shutdown on SIGTERM — Concurrent Systems Programming with Go
- Executing Concurrent Checks — Concurrent Systems Programming with Go