Channels
Send typed values between goroutines and synchronize their work with channels.
A channel passes values between goroutines, functions that can run while their caller continues. An unbuffered send waits until another goroutine is ready to receive, so both sides meet at that send and receive.
Create a channel with make(chan T), where T is the type of value it carries. Send with ch <- value and receive with value := <-ch.
package main
import "fmt"
func main() {
ch := make(chan int)
go func() { ch <- 42 }()
value := <-ch
fmt.Println(value)
}In production
The sender that knows no more values will be sent should close the channel. Sending to a closed channel causes a panic, an unexpected failure that stops normal execution. Receiving after a closed channel is empty returns the default value for its type, such as 0 for an integer. Use the two-result receive form to check whether a value was actually sent.