mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-19 03:31:16 +00:00
first
This commit is contained in:
47
course/13-channels/exercises/5-close/complete.go
Normal file
47
course/13-channels/exercises/5-close/complete.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func countReports(numSentCh chan int) int {
|
||||
total := 0
|
||||
for {
|
||||
numSent, ok := <-numSentCh
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
total += numSent
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
// TEST SUITE - Don't touch below this line
|
||||
|
||||
func test(numBatches int) {
|
||||
numSentCh := make(chan int)
|
||||
go sendReports(numBatches, numSentCh)
|
||||
|
||||
fmt.Println("Start counting...")
|
||||
numReports := countReports(numSentCh)
|
||||
fmt.Printf("%v reports sent!\n", numReports)
|
||||
fmt.Println("========================")
|
||||
}
|
||||
|
||||
func main() {
|
||||
test(3)
|
||||
test(4)
|
||||
test(5)
|
||||
test(6)
|
||||
}
|
||||
|
||||
func sendReports(numBatches int, ch chan int) {
|
||||
for i := 0; i < numBatches; i++ {
|
||||
numReports := i*23 + 32%17
|
||||
ch <- numReports
|
||||
fmt.Printf("Sent batch of %v reports\n", numReports)
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
}
|
||||
close(ch)
|
||||
}
|
||||
Reference in New Issue
Block a user