closing channels
This commit is contained in:
parent
de85f000de
commit
1f30b700bd
33
src/039-closing-channels/closing-channels.go
Normal file
33
src/039-closing-channels/closing-channels.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
jobs := make(chan bool, 5)
|
||||||
|
done := make(chan bool)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
_, more := <- jobs
|
||||||
|
if more {
|
||||||
|
fmt.Println("received job")
|
||||||
|
} else {
|
||||||
|
fmt.Println("received all")
|
||||||
|
done <- true
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
for i := 0; i < 5 ; i++ {
|
||||||
|
jobs <- false
|
||||||
|
fmt.Println("sent job")
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
close(jobs)
|
||||||
|
fmt.Println("sent all")
|
||||||
|
|
||||||
|
<- done
|
||||||
|
}
|
@ -44,7 +44,7 @@ scatter-gather
|
|||||||
rate-limiting
|
rate-limiting
|
||||||
worker-pools
|
worker-pools
|
||||||
nonblocking-channel-operations ~
|
nonblocking-channel-operations ~
|
||||||
closing-channels ~
|
closing-channels
|
||||||
timers
|
timers
|
||||||
tickers
|
tickers
|
||||||
state-goroutine
|
state-goroutine
|
||||||
|
Loading…
x
Reference in New Issue
Block a user