blob: c8127644b037813fcc01b1542a2bdd71a5f3df09 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package main
import (
"flag"
"fmt"
"time"
)
type Ball struct{ hits int }
func main() {
numSecs := flag.Int("s", 1, "Number of seconds game should last")
flag.Parse()
game(*numSecs)
}
func game(numSecs int) {
table := make(chan Ball)
go player("ping", table)
go player("pong", table)
table <- Ball{}
// Make the game last this long.
time.Sleep(time.Duration(numSecs) * time.Second)
// Game over: grab the ball.
<-table
}
func player(name string, table chan Ball) {
for {
ball := <-table
ball.hits++
fmt.Println(name, ball.hits)
// Simulate some contact with the paddle.
time.Sleep(100 * time.Millisecond)
table <- ball
}
}
|