publish channel-buffering
This commit is contained in:
parent
f79d9e151f
commit
86867c9cff
@ -26,7 +26,7 @@ Recursion
|
||||
# OK Guards
|
||||
Goroutines
|
||||
Channels
|
||||
# Channel Buffering
|
||||
Channel Buffering
|
||||
# Channel Directions
|
||||
# Synchronization
|
||||
Select
|
||||
|
@ -1,10 +1,27 @@
|
||||
// By default channels are _unbuffered_, meaning that they
|
||||
// will only accept sends (`chan <-`) if there is a
|
||||
// corresponding receive (`<- chan`) ready to receive the
|
||||
// sent value. _Buffered channels_ accept a limited
|
||||
// number of values without a corresponding receiver for
|
||||
// those values.
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var messages chan string = make(chan string, 1)
|
||||
messages <- "ping"
|
||||
msg := <-messages
|
||||
fmt.Println(msg)
|
||||
|
||||
// Here we `make` a channel of strings buffering up to
|
||||
// 2 values.
|
||||
messages := make(chan string, 2)
|
||||
|
||||
// Because this channel is buffered, we can send these
|
||||
// values into the channel without a corresponding
|
||||
// concurrent receive.
|
||||
messages <- "buffered"
|
||||
messages <- "channel"
|
||||
|
||||
// Later we can receive these two values as usual.
|
||||
fmt.Println(<-messages)
|
||||
fmt.Println(<-messages)
|
||||
}
|
||||
|
3
examples/channel-buffering/channel-buffering.sh
Normal file
3
examples/channel-buffering/channel-buffering.sh
Normal file
@ -0,0 +1,3 @@
|
||||
$ go run channel-buffering.go
|
||||
buffered
|
||||
channel
|
Loading…
x
Reference in New Issue
Block a user