Skip to content

Instantly share code, notes, and snippets.

@ilovejs
Created October 1, 2014 09:18
Show Gist options
  • Save ilovejs/e2fed71f738c6f04614e to your computer and use it in GitHub Desktop.
Save ilovejs/e2fed71f738c6f04614e to your computer and use it in GitHub Desktop.
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)
//add ball
table <- new(Ball) // game on; toss the ball
time.Sleep(1 * time.Second)
//remove ball
<-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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment