Concurrency Basics
Buffered Channel
A buffered channel can hold a small value before it is received.
Buffered Channel
buffered_channel.go
package main
import "fmt"
func main() {
var second =
queue := make(chan string, 2)
queue <- "red"
queue <- second
firstOut := <-queue
secondOut := <-queue
fmt.Println("first=", firstOut)
fmt.Println("second=", secondOut)
fmt.Println("capacity=", cap(queue))
}
package main
import "fmt"
func main() {
var second =
queue := make(chan string, 2)
queue <- "red"
queue <- second
firstOut := <-queue
secondOut := <-queue
fmt.Println("first=", firstOut)
fmt.Println("second=", secondOut)
fmt.Println("capacity=", cap(queue))
}
package main
import "fmt"
func main() {
var second =
queue := make(chan string, 2)
queue <- "red"
queue <- second
firstOut := <-queue
secondOut := <-queue
fmt.Println("first=", firstOut)
fmt.Println("second=", secondOut)
fmt.Println("capacity=", cap(queue))
}
buffered channel
A buffered channel can act as a deterministic handoff queue when its capacity is large enough.