March 2019
Beginner to intermediate
324 pages
7h 17m
English
To declare a channel in Go, you simply use the make keyword, as follows:
myChannel := make(chan int)
In the preceding code, we created and initialized a channel called myChannel that can hold int values. This channel can then be used to send an int value from one goroutine to another.
Here is how to receive a value from a channel:
//myIntValue will host the value received from the channelmyIntValue := <-myChannel
Here is how to send a value to a channel:
myChannel <- 4
Whenever you execute a send or a receive operation on a regular Go channel, your goroutine will block until the value is fully sent or received. This simply means that if you send a value via a channel but there is no other goroutine waiting for it on the ...
Read now
Unlock full access