Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Ping Pong in Go with go routines
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")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.