增加 Golang Chan 超时.

Signed-off-by: chen.yang <chen.yang@yuzhen-iot.com>
This commit is contained in:
chen.yang 2021-08-03 09:27:35 +08:00
parent fa33b05e7c
commit a8bc6197e8
1 changed files with 28 additions and 0 deletions

View File

@ -0,0 +1,28 @@
# Golang Chan 超时
```go
package main
import (
"fmt"
"time"
)
var ch chan int = make(chan int, 1)
func main() {
go aaa()
select {
case <-ch: //拿到锁
fmt.Println("call")
case <-time.After(5 * time.Second): //超时5s
fmt.Println("5 sec call")
}
}
func aaa() {
time.Sleep(time.Second * 3)
ch <- 1
}
```