summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go12
1 files changed, 10 insertions, 2 deletions
diff --git a/main.go b/main.go
index f561be4..663e6aa 100644
--- a/main.go
+++ b/main.go
@@ -5,6 +5,7 @@ import (
"fmt"
"log"
"math/rand"
+ "sync"
"time"
)
@@ -42,12 +43,13 @@ func game(numPlayers, faces int) {
// player took to hit the winning number.
scores := make(chan Score)
+ var wg sync.WaitGroup
for i := range numPlayers {
id := i + 1
fmt.Printf("Player %d start!\n", id)
// Spawn a player.
- go func() {
+ wg.Go(func() {
var score int
// Start rolling the dice!
@@ -58,13 +60,19 @@ func game(numPlayers, faces int) {
if outcome == winningNumber {
scores <- Score{id: id, score: score}
+ return
}
time.Sleep(1 * time.Second)
}
- }()
+ })
}
+ go func() {
+ wg.Wait()
+ close(scores)
+ }()
+
for score := range scores {
fmt.Printf("%v\n", score)
}