36 lines
384 B
Go
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)
|
|
}
|