From a8bc6197e8c0fd3b5df65236cbd449cfae8a3991 Mon Sep 17 00:00:00 2001 From: "chen.yang" Date: Tue, 3 Aug 2021 09:27:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20Golang=20Chan=20=E8=B6=85?= =?UTF-8?q?=E6=97=B6.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chen.yang --- .../Go/Basic/Chan/Golang_Chan_超时.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Software/Development/Language/Go/Basic/Chan/Golang_Chan_超时.md diff --git a/Software/Development/Language/Go/Basic/Chan/Golang_Chan_超时.md b/Software/Development/Language/Go/Basic/Chan/Golang_Chan_超时.md new file mode 100644 index 0000000..12601c9 --- /dev/null +++ b/Software/Development/Language/Go/Basic/Chan/Golang_Chan_超时.md @@ -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 +} +```