Skip to content

Channels

Channels are typed conduits that allow goroutines to communicate and synchronize. You send a value into a channel using ch <- v and receive a value using v := <-ch. By default, channels are unbuffered: sends block until a receiver is ready, and vice versa.

Create a channel and pass a value between two goroutines.

package main
import "fmt"
func main() {
ch := make(chan string)
go func() {
ch <- "ping"
}()
msg := <-ch
fmt.Println(msg)
}
Terminal window
ping