Mastering-Go-Second-Edition/ch10/timeOut1.go
2019-01-10 22:09:57 +02:00

34 lines
470 B
Go

package main
import (
"fmt"
"time"
)
func main() {
c1 := make(chan string)
go func() {
time.Sleep(time.Second * 3)
c1 <- "c1 OK"
}()
select {
case res := <-c1:
fmt.Println(res)
case <-time.After(time.Second * 1):
fmt.Println("timeout c1")
}
c2 := make(chan string)
go func() {
time.Sleep(3 * time.Second)
c2 <- "c2 OK"
}()
select {
case res := <-c2:
fmt.Println(res)
case <-time.After(4 * time.Second):
fmt.Println("timeout c2")
}
}