August 2019
Beginner to intermediate
798 pages
17h 2m
English
In this subsection, you will learn what happens when you try to read from a closed channel using the Go code found in readClose.go, which is going to be presented in two parts.
The first part of readClose.go is as follows:
package main
import (
"fmt"
)
func main() {
willClose := make(chan int, 10)
willClose <- -1
willClose <- 0
willClose <- 2
<-willClose
<-willClose
<-willClose
In this part of the program, we create a new int channel named willClose, we write data to it, and we read all that data without doing anything with it.
The second part of readClose.go contains the following code:
close(willClose)
read := <-willClose
fmt.Println(read)
}
In this part, we close the willClose channel and we try to read ...
Read now
Unlock full access