
goLang Learning Blocks ~ Buffered Channels
Channel
Channel in Go provides a connection between two goroutines, allowing them to communicate and synchronize the exchange of any resource that is passed through it.
It is the channel’s ability to control the goroutines interaction that creates the synchronization mechanism.
When a channel is created with no capacity, it is called an unbuffered channel. In turn, a channel created with capacity is called a buffered channel.
Buffered channels
Buffered channels have a capacity and therefore can behave a bit differently. When a goroutine attempts to send a resource to a buffered channel and the channel is full, the channel will lock the goroutine and make it wait until a buffer becomes available. If there is room in the channel, the send can take place immediately and the goroutine can move on.
When a goroutine attempts to receive from a buffered channel and the buffered channel is empty, the channel will lock the goroutine and make it wait until a resource has been sent. If the buffer is full or if there is nothing to receive, a buffered channel will behave very much like an unbuffered channel.
Example
package main
import "fmt"
func main() {
c := make(chan int, 2)
c <- 40 // sender to channel, sync is not required
c <- 45
// c <- 48 this will block the channel
fmt.Print(<-c, <-c) // receiver
}Leave goLang Learning Blocks ~ Buffered Channels to:
Read more #devdlearningblogs posts
Best Posts From _devdcaptures
We have not curated any of devrajsinghrawat's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From _devdcaptures
- Programmer Analysis on Facebook Libra
- HyperLedger Fabric - Setting and Testing Raft based Orderer
- Facebook Libra
- India’s Own Space Station ! What does it Mean ?
- Taxibots on wheels to save fuel at airport !!
- UPI 2.0
- Sweden is building an electric road that will charge car as you drive
- Dr Subramanian Swamy Speaks To Republic TV After ED Attaches National Herald Properties
- Elon Musk Challenger? Super Sonic Hydrogen IC Engine
- goLang Learning Blocks ~ Concurrency design pattern using Channels