summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go33
1 files changed, 22 insertions, 11 deletions
diff --git a/main.go b/main.go
index a4670c0..e5d4c11 100644
--- a/main.go
+++ b/main.go
@@ -18,11 +18,10 @@ func main() {
func game(numSecs int) {
var wg sync.WaitGroup
- done := make(chan struct{})
p0 := make(chan Ball)
- p1 := player(1, &wg, done, p0)
- p2 := player(2, &wg, done, p1)
+ p1 := player(1, &wg, p0)
+ p2 := player(2, &wg, p1)
t := time.Tick(time.Duration(numSecs) * time.Second)
p0 <- Ball{}
@@ -36,13 +35,29 @@ loop:
}
}
+ // This sets off a chain reaction that closes all the
+ // p-channels (p0, p1, etc.)
close(p0)
wg.Wait()
- fmt.Println("Done for now")
+
+ // If any of these prints out, we know we did something wrong.
+ for range p0 {
+ fmt.Println("P0")
+ }
+
+ for range p1 {
+ fmt.Println("P1")
+ }
+
+ for range p2 {
+ fmt.Println("P2")
+ }
+
+ fmt.Println("Done for real!")
}
-func player(id int, wg *sync.WaitGroup, done <-chan struct{}, input <-chan Ball) <-chan Ball {
+func player(id int, wg *sync.WaitGroup, input <-chan Ball) <-chan Ball {
out := make(chan Ball)
wg.Go(func() {
@@ -52,17 +67,13 @@ func player(id int, wg *sync.WaitGroup, done <-chan struct{}, input <-chan Ball)
}()
fmt.Printf("(%d) started\n", id)
- loop:
+
for b := range input {
b.hits++
fmt.Printf("(%d) %d\n", id, b.hits)
time.Sleep(100 * time.Millisecond)
- select {
- case out <- b:
- case <-done:
- break loop
- }
+ out <- b
}
})