Created
May 14, 2024 10:47
-
-
Save nozim/dd1a69f83430758b70db4e985c923a22 to your computer and use it in GitHub Desktop.
pingponggo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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