September 2017
Intermediate to advanced
466 pages
9h 33m
English
This subsection will improve the Go code of writeChannel.go by allowing you to read from a channel. The presented program will be called readChannel.go and be presented in four parts.
The first part is the following:
package main import ( "fmt" "time" )
The second part of readChannel.go has the following Go code:
func writeChannel(c chan<- int, x int) {
fmt.Println(x)
c <- x
close(c)
fmt.Println(x)
}
Once again, note that if nobody collects the data written to a channel, the function that sent it will stall while waiting for someone to read its data. However, in Chapter 10, Goroutines - Advanced Features, you will see a very pretty solution to this problem.
The third part has the following Go code:
func main() { c ...Read now
Unlock full access