- Hands-On Full Stack Development with Go
- Mina Andrawos
- 398字
- 2021-07-02 12:33:34
The select statement
The select statement is an important construct in Go. It allows you to control multiple channels at the same time. With select, you can send or receive values to different channels, and then execute code based on the channel that unblocks the first.
This will be best explained by an example; let's take a look at the following piece of code:
select {
case i := <-ch:
fmt.Println("Received value:", i)
case <-time.After(1 * time.Second):
fmt.Println("timed out")
}
In the preceding example, we utilized the select statement to exercise control over two different channels. The first channel is called ch, which we attempted to receive a value from. In comparison, the second channel is produced from the time.After() function. The time.After() function is very popular in Go, and especially in select statements. The function generates a channel that only receives a value after the specified timeout, in effect producing a blocking channel for a predefined period of time. You can use time.After() in your code with the select statement in cases where you would want to timeout a receive or a send operation on another channel.
Here is another example of sending a select statement with a timeout channel, but this time it's a combination of receive and send operations:
select {
case i := <-ch1:
fmt.Println("Received value on channel ch1:", i)
case ch2 <- 10:
fmt.Println("Sent value of 10 to channel ch2")
case <-time.After(1 * time.Second):
fmt.Println("timed out")
}
The preceding code will synchronize between three channels: ch1, ch2, and the time.After() channel. The select statement will wait on those three channels and then, depending on whichever channel finishes first, the appropriate select case will be executed.
The select statement also supports the default case. The default case will execute immediately if none of the channels are ready; here is an example:
select {
case i := <-ch1:
fmt.Println("Received value on channel ch1:", i)
case ch2 <- 10:
fmt.Println("Sent value of 10 to channel ch2")
default:
fmt.Println("No channel is ready")
}
In the preceding code, if both ch1 and ch2 are blocked by time, the select statement is executed, and then the default case will trigger.
If multiple channels finish at the same time while being controlled by a select statement, the channel case to be executed is picked at random.
Let's take a look at the sync package in the next section.
- 大話PLC(輕松動漫版)
- ASP.NET Core:Cloud-ready,Enterprise Web Application Development
- JavaScript+jQuery開發(fā)實戰(zhàn)
- Microsoft System Center Orchestrator 2012 R2 Essentials
- Cocos2d-x學習筆記:完全掌握Lua API與游戲項目開發(fā) (未來書庫)
- Visual Basic程序設(shè)計實踐教程
- PLC應用技術(shù)(三菱FX2N系列)
- 零基礎(chǔ)學Kotlin之Android項目開發(fā)實戰(zhàn)
- Android嵌入式系統(tǒng)程序開發(fā):基于Cortex-A8(第2版)
- HTML5+CSS3+jQuery Mobile APP與移動網(wǎng)站設(shè)計從入門到精通
- Secret Recipes of the Python Ninja
- INSTANT Apache Hive Essentials How-to
- Microsoft Exchange Server 2016 PowerShell Cookbook(Fourth Edition)
- Raspberry Pi開發(fā)實戰(zhàn)
- Visual Basic 開發(fā)從入門到精通