mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 12:32:21 +00:00
ch-1-06-部分例子代码统一用tab缩进
This commit is contained in:
parent
7218960bf0
commit
7a64a27beb
@ -513,23 +513,23 @@ Go语言中不同Goroutine之间主要依靠管道进行通信和同步。要同
|
||||
基于`select`实现的管道的超时判断:
|
||||
|
||||
```go
|
||||
select {
|
||||
case v := <-in:
|
||||
select {
|
||||
case v := <-in:
|
||||
fmt.Println(v)
|
||||
case <-time.After(time.Second):
|
||||
case <-time.After(time.Second):
|
||||
return // 超时
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
通过`select`的`default`分支实现非阻塞的管道发送或接收操作:
|
||||
|
||||
```go
|
||||
select {
|
||||
case v := <-in:
|
||||
select {
|
||||
case v := <-in:
|
||||
fmt.Println(v)
|
||||
default:
|
||||
default:
|
||||
// 没有数据
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
通过`select`来阻止`main`函数退出:
|
||||
|
Loading…
x
Reference in New Issue
Block a user