Created
November 5, 2013 20:22
-
-
Save ardeshir/7325545 to your computer and use it in GitHub Desktop.
Simple Go routine Example
This file contains 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" | |
"time" | |
) | |
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 | |
fmt.Printf("Game over!\n") | |
} | |
func player (name string, table chan * Ball) { | |
for { | |
ball := <-table | |
ball.hits++ | |
fmt.Println(name, ball.hits) | |
time.Sleep(100 * time.Millisecond) | |
table <- ball | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment