Go example--Timeout processing

Source: Internet
Author: User

Tags: import amp sleep res ack case class execute out

package mainimport (    "fmt"    "time")func main() {    c1 := make(chan string, 1)    go func() {        time.Sleep(time.Second * 2)        c1 <- "result 1"    }()    select {    case res := <-c1:        fmt.Println(res)        //延时器,当上个case超时执行这个case    case <-time.After(time.Second * 1):        fmt.Println("timeout 1")    }    c2 := make(chan string, 1)    go func() {        time.Sleep(time.Second * 2)        c2 <- "result 2"    }()    select {    case res := <-c2:        fmt.Println(res)    case <-time.After(time.Second * 3):        fmt.Println("timeout 2")    }}

Go example--Timeout processing

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: