Skip to content

Instantly share code, notes, and snippets.

@esimov
Last active January 29, 2016 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esimov/dc5a365b66572d9d9a40 to your computer and use it in GitHub Desktop.
Save esimov/dc5a365b66572d9d9a40 to your computer and use it in GitHub Desktop.
Ping-pong goroutine pattern
package main
import (
"time"
"fmt"
)
func main() {
var ball int
ch := make(chan int)
for i:= 0; i < 10; i++ {
go func(){
go PingPong(ch)
}()
}
ch <- ball
time.Sleep(1 * time.Second)
<-ch
}
func PingPong(ch chan int) {
for {
ball := <-ch
ball++
fmt.Println(ball)
time.Sleep(1 * time.Second)
ch <- ball
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment