x
 
1
// By default channels are _unbuffered_, meaning that they
2
// will only accept sends (`chan <-`) if there is a
3
// corresponding receive (`<- chan`) ready to receive the
4
// sent value. _Buffered channels_ accept a limited
5
// number of  values without a corresponding receiver for
6
// those values.
7
8
package main
9
10
import "fmt"
11
12
func main() {
13
14
    // Here we `make` a channel of strings buffering up to
15
    // 2 values.
16
    messages := make(chan string, 2)
17
18
    // Because this channel is buffered, we can send these
19
    // values into the channel without a corresponding
20
    // concurrent receive.
21
    messages <- "buffered"
22
    messages <- "channel"
23
24
    // Later we can receive these two values as usual.
25
    fmt.Println(<-messages)
26
    fmt.Println(<-messages)
27
}
28
分享