This is a creation in
Article, where the information may have evolved or changed.
Reprinted from: http://yanyiwu.com/work/2014/11/08/golang-select-typical-usage.html
The function of the Golang Select, similar to the Select, poll, Epoll, is to listen to the IO operation and trigger the corresponding action when the IO operation occurs.
Example:
CH1: = Make (chan int, 1)
CH2: = Make (chan int, 1)
...
Select {
Case <-CH1:
Fmt. Println ("ch1 pop One Element")
Case <-CH2:
Fmt. Println ("CH2 pop One Element")
}
Note that the code form for select is very similar to switch, but the action statement in Select's case can only be an "IO operation".
In this example, select waits until a case statement is completed, that is, until the data is read successfully from CH1 or CH2. The SELECT statement ends.
"Using Select to implement the timeout mechanism"
As follows:
Timeout: = Make (chan bool, 1)
Go func () {
Time. Sleep (1E9)//Sleep one second
Timeout <-True
}()
CH: = make (chan int)
Select {
Case <-Ch:
Case <-Timeout:
Fmt. Println ("timeout!")
}
When the time-out expires, the CASE2 operation succeeds. So the SELECT statement exits. Instead of always blocking the read operation on Ch. Thus, the time-out setting for CH read operation is realized.
The following is a more interesting point.
When the SELECT statement has default:
CH1: = Make (chan int, 1)
CH2: = Make (chan int, 1)
Select {
Case <-CH1:
Fmt. Println ("ch1 pop One Element")
Case <-CH2:
Fmt. Println ("CH2 pop One Element")
Default
Fmt. PRINTLN ("Default")
}
At this point, because both CH1 and CH2 are empty, neither CASE1 nor Case2 will read successfully. The select executes the default statement.
Because of this default feature, we can use the SELECT statement to detect if Chan is full.
As follows:
CH: = Make (chan int, 1)
CH <-1
Select {
Case CH <-2:
Default
Fmt. PRINTLN ("Channel is full!")
}
Because CH inserted 1 is full, when CH to insert 2, and found that Ch is full (case1 blocked), select executes the default statement. This allows for detection of whether the channel is full, rather than waiting.
For example, we have a service, and when the request comes in, we generate a job to throw into the channel, which is executed by other co-processes from the channel to get the job. But we hope that when the channel is concealed, the job is discarded and replied, "The service is busy, please try again." "You can use Select to implement this requirement.
About garbage collection
C + + wrote for a long time, just contact Golang when the most incomprehensible is why the author to support garbage collection. Whether it is from the implementation of the garbage collector, or the programmer's programming habits to develop aspects, can not be written out of the program performance loss. But after writing for a few days Golang, also feel that there is garbage collection really greatly reduce the programmer's mental burden, reduce the programming threshold, improve programming efficiency. It reminds me of the relationship between assembler and C, even though the performance of C is not as high as the compilation, but the latter is a subversion of the former.
Reference
Go Language programming
Go Language programming
Reprint Please specify source: Golang's Select typical usage