Skip to content

Instantly share code, notes, and snippets.

@nozim
Created May 14, 2024 10:47
Show Gist options
  • Save nozim/dd1a69f83430758b70db4e985c923a22 to your computer and use it in GitHub Desktop.
Save nozim/dd1a69f83430758b70db4e985c923a22 to your computer and use it in GitHub Desktop.
pingponggo
package main
import (
"fmt"
"math/rand"
"time"
)
type Ball struct{}
func main() {
ch := make(chan Ball)
go func() {
for b := range ch {
fmt.Println("PING...")
pause := rand.Intn(5)
time.Sleep(time.Duration(pause) * time.Second)
ch <- b
}
}()
c := 0
ball := Ball{}
go func() {
for {
ch <- ball
fmt.Println("PONG")
pause := rand.Intn(5)
time.Sleep(time.Duration(pause) * time.Second)
ball = <-ch
c++
}
}()
for {
time.Sleep(10 * time.Second)
if c == 10 {
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment