Channel Buffering
Use a fixed-size channel queue to let producers and consumers work at different speeds.
A channel passes values between goroutines, functions started with go that can run while their caller continues. A buffered channel adds a small queue of values waiting to be received. It holds a fixed number while the receiver is busy. A send waits only when the queue is full, and a receive waits only when it is empty.
The capacity is the second argument to make. These two sends finish before either receive because the channel can hold two values.
package main
import "fmt"
func main() {
ch := make(chan string, 2)
ch <- "first"
ch <- "second"
fmt.Println(<-ch)
fmt.Println(<-ch)
}In production
Choose a capacity based on how much waiting work the program can keep. A buffer can absorb a brief difference between how quickly values are sent and received, but it cannot fix a receiver that is always slower. len(ch) reports the queue size at that moment; another goroutine can change it immediately, so do not use it to decide whether a send is safe.