mirror of
https://github.com/MADTeacher/go_basics.git
synced 2025-11-23 21:34:47 +02:00
39 lines
628 B
Go
39 lines
628 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
ping := make(chan int, 1)
|
|
pong := make(chan int, 1)
|
|
|
|
ping <- 1
|
|
|
|
go func(ping, pong chan int) {
|
|
var ball int
|
|
for {
|
|
select {
|
|
case ball = <-ping:
|
|
fmt.Println("Ping", ball)
|
|
time.Sleep(500 * time.Millisecond)
|
|
if ball > 2 {
|
|
ping = nil
|
|
} else {
|
|
pong <- ball + 1
|
|
}
|
|
case ball = <-pong:
|
|
fmt.Println("Pong", ball)
|
|
time.Sleep(500 * time.Millisecond)
|
|
ping <- ball + 1
|
|
case value := <-time.After(time.Second):
|
|
fmt.Println("Time out!!!", value)
|
|
}
|
|
}
|
|
}(ping, pong)
|
|
|
|
time.Sleep(4 * time.Second)
|
|
fmt.Println("Exit")
|
|
}
|