Go Language Learning-time pack

Source: Internet
Author: User
Tags sleep function
This is a creation in Article, where the information may have evolved or changed.

Time Package for Go language

Composition

    • Time. Duration (duration, time-consuming)
    • Time. Time (dot)
    • Time. C (Put a point in the pipeline) [Time.c:=make (Chan time. Time)]

There are 2 things in the time bag, one is the point, the other is the length.
The point of time means "a moment", such as January 1, 2000 1:1 1 seconds at that moment (the background is recorded in Unix time, calculated from 1970 onwards)
Time is the difference between a moment and another moment, which is time consuming

Function

Sleep function

Time. Sleep (time. Duration)
Indicates how much time to sleep, when sleeping, is blocking the state

fmt.Println("start sleeping...")time.Sleep(time.Second)fmt.Println("end sleep.")//【结果】打印start sleeping后,等了正好1秒后,打印了end sleep//会阻塞,Sleep时,什么事情都不会做

After function

Time. After (time. Duration)
Same as sleep, meaning how long after, but not blocking before removing the contents of the pipe

Fmt.Println("the 1")tc:= Time. After(time. Second) //Returns a time. C This pipe, 1 seconds (time. Second), a point in time is placed in this pipeline. Now ())                        //Point-in-time record is the time value of the moment the pipe is placedFmt.Println("the 2")Fmt.Println("the 3")<-tc//block until the data in the TC pipe is removedFmt.Println("the 4")//"result" immediately print 123, wait 1 seconds less than a little time, print 4, Endafter printing the 1, an empty pipe is obtained, and the pipe will have data in 1 seconds .//Print the 2, (there are more things to do here)//Print the 3//Wait until the data of the pipe can be taken out (the time to remove the data is exactly 1 seconds from the time the TC pipe was obtained)//Print the 4

Fmt.Println("the 1")tc:= Time. After(time. Second) //Returns a time. C This pipe, 1 seconds (time. Second) will be placed in this pipeline after the                        //A point in time. Now ()), the point in time is the time value of the moment the pipe is placed .Fmt.Println("the 2")Fmt.Println("the 3") Time.Sleep(time. second*0.5)//Here is assuming that this println action executes for half a secondFmt.Println("the 4") Time.Sleep(time. second*0.5)//Here is assuming that this println action executes for half a secondFmt.Println("the 5")Fmt.Println("the 6")Fmt.Println("The 7")<-tc//block until the data in the TC pipe is removedFmt.Println("The 8")//"Results" now prints 1 and 2, took half a second to print 3 and 4, and then immediately printed 5678, ending//The <-TC here is immediately able to get the data//Because there is already data in the pipeline as early as the execution of almost print 6 .//When the Gorotine line drops the data into the pipeline, it blocks itself (see Goroutine for details)

上面的说法是错的,实际上会抱一个错误,0.5秒不能这样表示,然后打印是先打印1,2,3,过了半秒打印4,再过半秒答应5,6,7,8

Afterfunc function

Time. Afterfunc (time. Duration,func ())
Like after, meaning how long after the Goroutine line executes the function

f := func() {    fmt.Println("Time out")}time.AfterFunc(1*time.Second, f)time.Sleep(2 * time.Second) //要保证主线比子线“死的晚”,否则主线死了,子线也等于死了 //【结果】运行了1秒后,打印出timeout,又过了1秒,程序退出 //将一个间隔和一个函数给AfterFunc后 //间隔时间过后,执行传入的函数

Since the F function is not executed on main line, it is registered in Goroutine line.
So once you regret, you need to use the Stop command to stop the execution that is about to start, and it's too late to start executing.

houhui := truef := func() {    fmt.Println("Time out")}ta := time.AfterFunc(2*time.Second, f)time.Sleep(time.Second)if houhui {    ta.Stop()}time.Sleep(3 * time.Second)    //要保证主线比子线“死的晚”,否则主线死了,子线也等于死了 //【结果】运行了3秒多一点点后,程序退出,什么都不打印 //注册了个f函数,打算2秒后执行 //过了1秒后,后悔了,停掉(Stop)它

Tick function

Time. Tick (time. Duration)
Similar to After, meaning how often after the other is consistent with after

fmt.Println("the 1"//返回一个time.C这个管道,1秒(time.Second)后会在此管道中放入一个时间点,                        //1秒后再放一个,一直反复,时间点记录的是放入管道那一刻的时间for i:=1;i<=2;i++{    <-tc    fmt.Println("hello")}//每隔1秒,打印一个hello

Time. Time method (Time.time own unique function)

Before & After methods

Determines whether a point in time is in front (back) of another point in time, and returns TRUE or False

t1:= Time. Now() Time.Sleep(time. Second)t2:= Time. Now()a: =t2. After(t1)     Is the recording time of the//t2 in the back of the T1 record time * *, yes, A is trueFmt.Println(a)       //trueb: =t2.before(t1)     Is the recording time of the//t2 in front of the T1 record time * *, yes, B is trueFmt.Println(b)       //false

Sub method

Subtract two time points and get the difference (Duration)

t1:=time.Now()time.Sleep(time.Second)t2:=time.Now()d:=t2.Sub(t1)     //时间2减去时间1fmt.Println(d)       //打印结果差不多为1.000123几秒,因为Sleep无法做到精确的睡1秒后发生的时间  减去   先发生时间,是正数

Add method

Take a time point, add a time, get another point in time

t1:=time.Now()              //现在是12点整(假设),那t1记录的就是12点整t2:=t1.Add(time.Hour)          //那t1的时间点 **加上(Add)** 1个小时,是几点呢?

fmt.Println(t2) //13点(呵呵)

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.

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.