Created
March 4, 2015 14:38
-
-
Save SeanDongX/d098e644acd7868116b0 to your computer and use it in GitHub Desktop.
Go Concurrency Pattern - PingPong
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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