August 2019
Beginner to intermediate
798 pages
17h 2m
English
The code in this subsection will teach you how to write to a channel. Writing the value x to channel c is as easy as writing c <- x. The arrow shows the direction of the value, and you will have no problem with this statement as long as both x and c have the same type. The example code in this section is saved in writeCh.go, and it will be presented in three parts.
The first code segment from writeCh.go is as follows:
package main
import (
"fmt"
"time"
)
func writeToChannel(c chan int, x int) {
fmt.Println(x)
c <- x
close(c)
fmt.Println(x) }
The chan keyword is used for declaring that the c function parameter will be a channel, and it should be followed by the type of the channel (int). The c <- x statement allows you ...
Read now
Unlock full access