1
0
Fork 0
mirror of https://github.com/vbatts/talks.git synced 2024-11-16 05:28:37 +00:00
talks/2015/02-devconf.cz/pingpong.go
2015-02-06 01:04:10 +01:00

33 lines
507 B
Go

// +build OMIT
package main
import (
"fmt"
"time"
)
// STARTMAIN1 OMIT
type Ball struct{ hits int }
func main() {
table := make(chan *Ball)
go player("ping", table)
go player("pong", table)
table <- new(Ball) // game on; toss the ball
time.Sleep(1 * time.Second)
<-table // game over; grab the ball
}
func player(name string, table chan *Ball) {
for {
ball := <-table
ball.hits++
fmt.Println(name, ball.hits)
time.Sleep(100 * time.Millisecond)
table <- ball
}
}
// STOPMAIN1 OMIT