O'Reilly logo

Distributed Computing with Go by V.N. Nikhil Anurag

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The unbuffered channel

This is the basic channel type available in Go. It is quite straightforward to use—we send data into the channel and we receive data at the other end. The interesting part is that any goroutine operating on an unbuffered channel will be blocked until both the sender and receiver goroutines are available. For example, consider the following code snippet:

ch := make(chan int) 
go func() {ch <- 100}     // Send 100 into channel.                                             Channel: send100          
go func() {val := <- ch}  // Goroutine waiting on channel.                                     Channel: recv1         
go func() {val := <- ch}  // Another goroutine waiting on channel.                             Channel: recv2

We have a channel ch of element type int. We start three goroutines; one sends a message of 100 onto the channel (send100) and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required