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 := <-values

Related terms

Taught in

References