nil Channels

Let’s learn about nil channels.

The use of nil channels

nil channels always block! Therefore, we should only use them when we actually want that behavior.

Coding example

The code that follows illustrates nil channels:

Press + to interact
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
var wg sync.WaitGroup
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)
wg.Done()
}
}
}
func send(c chan int) {
for {
c <- rand.Intn(10)
}
}
func main() {
c := make(chan int)
rand.Seed(time.Now().Unix())
wg.Add(1)
go add(c)
go send(c)
wg.Wait()
}

We are ...