package main import ( "flag" "fmt" "log" "sync" "time" ) type Ball struct{ hits int } func main() { numSecs := flag.Int("s", 1, "Number of seconds game should last") numPlayers := flag.Int("p", 2, "Number of players") flag.Parse() game(*numSecs, *numPlayers) } func game(numSecs, numPlayers int) { var wg sync.WaitGroup players := make([]chan Ball, numPlayers+1) players[0] = make(chan Ball) for i := range numPlayers { players[i+1] = player(i+1, &wg, players[i]) } t := time.Tick(time.Duration(numSecs) * time.Second) players[0] <- Ball{} loop: for b := range players[len(players)-1] { select { case players[0] <- b: case <-t: break loop } } // This sets off a chain reaction that closes the other // p-channels (p1, p2, etc.) close(players[0]) // Wait for all of the in-flight player goroutines to // finish. You need this! wg.Wait() // If any of these prints out, we know we did something wrong. for i, p := range players { for range p { log.Fatalf("P%d", i) } } fmt.Println("Done for real!") } // player spawns a new player goroutine that reads from the input // channel. Player returns a channel for listening for the goroutine's // output. Both channels are technically receive-only, but making them // bidirectional simplifies making them members of a slice which // contains a bidirectional channel. func player(id int, wg *sync.WaitGroup, input chan Ball) chan Ball { out := make(chan Ball) wg.Go(func() { defer func() { close(out) fmt.Printf("(%d) finished\n", id) }() fmt.Printf("(%d) started\n", id) for b := range input { b.hits++ fmt.Printf("(%d) %d\n", id, b.hits) time.Sleep(100 * time.Millisecond) out <- b } }) return out }