February 2018
Intermediate to advanced
246 pages
6h 3m
English
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 ...
Read now
Unlock full access