Skip to content

Instantly share code, notes, and snippets.

@akshaybharambe14
Created June 10, 2020 05:46
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 akshaybharambe14/0cd65a0b15b1175bfd45989d2afce5eb to your computer and use it in GitHub Desktop.
Save akshaybharambe14/0cd65a0b15b1175bfd45989d2afce5eb to your computer and use it in GitHub Desktop.
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