Go example--Channel Selector

Source: Internet
Author: User

Tags: sleep time data get UNC channel i++ end Port

package mainimport (    "fmt"    "time")func main() {    c1 := make(chan string)    c2 := make(chan string)    go func() {        time.Sleep(time.Second * 1)        c1 <- "one"    }()    go func() {        time.Sleep(time.Second * 2)        c2 <- "two"    }()    for i := 0; i < 2; i++ {        //通道选择器同时等待多个通道操作。        select {        //哪个通道获得数据就执行哪个case,select结束        case msg1 := <-c1:            fmt.Println(msg1)        case msg2 := <-c2:            fmt.Println(msg2)        }    }}

Go example--Channel Selector

Related Article

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.

Tags Index: