Skip to content

Instantly share code, notes, and snippets.

@SeanDongX
Created March 4, 2015 14:38
Show Gist options
  • Select an option

  • Save SeanDongX/d098e644acd7868116b0 to your computer and use it in GitHub Desktop.

Select an option

Save SeanDongX/d098e644acd7868116b0 to your computer and use it in GitHub Desktop.
Go Concurrency Pattern - PingPong
package main
import "fmt"
import "time"
type Ball struct {
hits int
}
func player(name string, table chan *Ball) {
for {
ball := <-table
ball.hits++
fmt.Println(name, ball.hits)
time.Sleep(100 * time.Millisecond)
table <- ball
}
}
func main() {
table := make(chan *Ball)
go player("ping", table)
go player("pong", table)
table <- new(Ball)
time.Sleep(1200 * time.Millisecond)
<-table
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment