August 2019
Beginner to intermediate
798 pages
17h 2m
English
In this subsection, you will learn about nil channels. These are a special kind of channel because they always block. They are illustrated in nilChannel.go, which will be presented in four code segments.
The first part of nilChannel.go is as follows:
package main
import (
"fmt"
"math/rand"
"time"
)
The second code portion of nilChannel.go is shown in the following Go code:
func add(c chan int) {
sum := 0
t := time.NewTimer(time.Second)
for {
select {
case input := <-c:
sum = sum + input
case <-t.C:
c = nil
fmt.Println(sum)
}
}
}
The add() function demonstrates how a nil channel is used. The <-t.C statement blocks the c channel of the t timer for the time that is specified in the time.NewTimer() call. Do not confuse channel ...
Read now
Unlock full access