mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-10 15:21:18 +00:00
58 lines
956 B
Go
58 lines
956 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
func pingPong(numPings int) {
|
|
pings := make(chan struct{})
|
|
pongs := make(chan struct{})
|
|
go ponger(pings, pongs)
|
|
pinger(pings, pongs, numPings)
|
|
}
|
|
|
|
// TEST SUITE - Don't touch below this line
|
|
|
|
func pinger(pings, pongs chan struct{}, numPings int) {
|
|
go func() {
|
|
sleepTime := 50 * time.Millisecond
|
|
for i := 0; i < numPings; i++ {
|
|
fmt.Println("ping", i, "sent")
|
|
pings <- struct{}{}
|
|
time.Sleep(sleepTime)
|
|
sleepTime *= 2
|
|
}
|
|
close(pings)
|
|
}()
|
|
i := 0
|
|
for range pongs {
|
|
fmt.Println("pong", i, "got")
|
|
i++
|
|
}
|
|
fmt.Println("pongs done")
|
|
}
|
|
|
|
func ponger(pings, pongs chan struct{}) {
|
|
i := 0
|
|
for range pings {
|
|
fmt.Println("ping", i, "got", "pong", i, "sent")
|
|
pongs <- struct{}{}
|
|
i++
|
|
}
|
|
fmt.Println("pings done")
|
|
close(pongs)
|
|
}
|
|
|
|
func test(numPings int) {
|
|
fmt.Println("Starting game...")
|
|
pingPong(numPings)
|
|
fmt.Println("===== Game over =====")
|
|
}
|
|
|
|
func main() {
|
|
test(4)
|
|
test(3)
|
|
test(2)
|
|
}
|