Mastering-Go-Second-Edition/ch10/nilChannel.go
2019-01-10 22:09:57 +02:00

36 lines
384 B
Go

package main
import (
"fmt"
"math/rand"
"time"
)
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)
}
}
}
func send(c chan int) {
for {
c <- rand.Intn(10)
}
}
func main() {
c := make(chan int)
go add(c)
go send(c)
time.Sleep(3 * time.Second)
}