A little experience of Select Case in Golang

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.

Look at the code first:

package mainimport (    "fmt"    "runtime")func main() {    runtime.GOMAXPROCS(1)    int_chan := make(chan int, 1)    string_chan := make(chan string, 1)    int_chan <- 1    string_chan <- "hello"    select {    case value := <-int_chan:        fmt.Println(value)    case value := <-string_chan:        panic(value)    }    fmt.Println(123)}

Run two times and the results are:

E:\go_study>go run 4.go1123E:\go_study>go run 4.go1123E:\go_study>go run 4.go1123E:\go_study>go run 4.go1123E:\go_study>go run 4.gopanic: hellogoroutine 1 [running]:main.main()        E:/go_study/4.go:18 +0x2f4exit status 2E:\go_study>

Found no, if two cases are satisfied with the condition, it is pseudo-random select one to execute, rather than the previous thought of the top-to-bottom to determine which case can be executed.

Also, when a case is executed, it exits select because it prints 123.

Finally, if no case can be executed, execute default immediately, then exit Select.

Reference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.