Introduction
Channels let different parts of a Go program talk to each other safely and easily.
ch := make(chan Type)
// or for a buffered channel
ch := make(chan Type, bufferSize)make to create a channel before using it.ch := make(chan int)ch := make(chan string, 3)package main import ( "fmt" ) func main() { // Create a channel for integers ch := make(chan int) // Start a new task to send a number go func() { ch <- 42 // send 42 to the channel }() // Receive the number from the channel num := <-ch fmt.Println("Received number:", num) }
make before using them.make(chan Type).ch := make(chan int, 2) ch <- 10 ch <- 20 fmt.Println(<-ch) fmt.Println(<-ch)
ch := make(chan int) ch <- 5 fmt.Println(<-ch)
Pass the test and this pattern moves unknown