patterngoCritical
What is channel buffer size?
Viewed 0 times
sizewhatchannelbuffer
Problem
I'm trying to create an asynchronous channel and I've been looking at http://golang.org/ref/spec#Making_slices_maps_and_channels.
What does it mean that the buffer size is 10? What specifically does the buffer size represent/limit?
c := make(chan int, 10) // channel with a buffer size of 10What does it mean that the buffer size is 10? What specifically does the buffer size represent/limit?
Solution
The buffer size is the number of elements that can be sent to the channel without the send blocking. By default, a channel has a buffer size of 0 (you get this with
make(chan int)). This means that every single send will block until another goroutine receives from the channel. A channel of buffer size 1 can hold 1 element until sending blocks, so you'd getc := make(chan int, 1)
c <- 1 // doesn't block
c <- 2 // blocks until another goroutine receives from the channelCode Snippets
c := make(chan int, 1)
c <- 1 // doesn't block
c <- 2 // blocks until another goroutine receives from the channelContext
Stack Overflow Q#11943841, score: 215
Revisions (0)
No revisions yet.