Skip to content

Channel buffering

Buffered channels have a capacity. Sends to a buffered channel block only when the buffer is full; receives block when the buffer is empty. Use make(chan T, capacity).

Send three values to a buffered channel without an immediate receiver.

package main
import "fmt"
func main() {
ch := make(chan string, 2)
ch <- "buffered"
ch <- "channel"
fmt.Println(<-ch)
fmt.Println(<-ch)
}
Terminal window
buffered
channel