Essential Go Basic types  Suggest an edit


Channels are typed queues used for goroutines to communicate with each other in a thread-safe manner.

Zero value of a channel is nil.

Channel basics

// create unbuffered channel of int values with capacity of 1
ch := make(chan int)
// start a new goroutine that sends value 3 over a channel
go func() { ch <- 3 }()
// read the value from a channel
// it waits until goroutine above sends a value
n := <-ch
fmt.Printf("n: %d\n", n)
n: 3

Learn more about channels.

  ↑ ↓ to navigate     ↵ to select     Esc to close