Go language--time. After ()

Source: Internet
Author: User

Go language--time. After () 78873396

1. Source Code Analysis:

After waits for the duration to elapse and then sends the current time
On the returned channel.
It's equivalent to Newtimer (d). C.
The underlying Timer isn't recovered by the garbage collector
Until the timer fires. If efficiency is a concern, use Newtimer
Instead and Timer.stop if the Timer is no longer needed.
Func after (d Duration) <-chan time {
Return Newtimer (d). C
}

According to the note, after waiting for a given time, the return value is sent to the current time, and the return value is a one-way read-only channel

2.demo:

func main() {    c := make(chan int, 1)    select {    case m := <-c:        fmt.Println(m)    case d := <-time.After(5 * time.Second):        fmt.Println("time out")        fmt.Println("current Time :", d)    } }

Time Out
Current time:2017-12-22 15:04:05.0462009 +0800 CST m=+5.004000001
3. Analysis:

In the first case, channel C has been in a blocking state; When after () is finished, a value is taken from the return value channel given D, which is the current time

Go language--time. After ()

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.