August 2019
Beginner to intermediate
798 pages
17h 2m
English
In this subsection, you will learn how to read from a channel. You can read a single value from a channel named c by executing <-c. In this case, the direction is from the channel to the outer world.
The name of the program that will be used to help you understand how to read from a channel is readCh.go, and it will be presented in three parts.
The first code segment from readCh.go is shown in the following Go code:
package main
import (
"fmt"
"time"
)
func writeToChannel(c chan int, x int) {
fmt.Println("1", x)
c <- x
close(c)
fmt.Println("2", x)
}
The implementation of the writeToChannel() function is the same as before.
The second part of readCh.go follows:
func main() { c := make(chan int) go writeToChannel(c, ...Read now
Unlock full access