package main import ( "fmt" "time" ) func main() { c := make(chan struct{}) go ping(c) go pong(c) // start the game c <- struct{}{} time.Sleep(time.Second * 10) // stop the game after 10 sec <-c } func play(c chan struct{}, player string) { <-c fmt.Println(player) time.Sleep(time.Second) c <- struct{}{} } func ping(c chan struct{}) { for { play(c, "Ping") } } func pong(c chan struct{}) { for { play(c, "Pong") } }