Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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