September 2017
Intermediate to advanced
466 pages
9h 33m
English
In this subsection, you will learn how to write to a channel. The presented program will be called writeChannel.go and you will see it in three parts.
The first part has the expected preamble:
package main import ( "fmt" "time" )
As you can understand, the use of channels does not require any extra Go packages.
The second part of writeChannel.go is the following:
func writeChannel(c chan<- int, x int) {
fmt.Println(x)
c <- x
close(c)
fmt.Println(x)
}
Although the writeChannel() function writes to the channel, the data will be lost because currently nobody reads the channel in the program.
The last part of the program contains the following Go code:
func main() { c := make(chan int) go writeChannel(c, 10) time.Sleep(2 ...Read now
Unlock full access