This is a creation in Article, where the information may have evolved or changed.
Time package includes two types of times: Point in time (a moment) and often (a certain period of time)
1. Time Constants (time formatting)
Const ( ansic = "Mon Jan _2 15:04:05 2006" unixdate = "Mon Jan _2 15:04:05 MST 2006" rubydate = "M On Jan 15:04:05-0700 2006 " RFC822 =" 15:04 MST " rfc822z =" Jan 15:04-0700 "//RFC822 With numeric zone RFC850 = "Monday, 02-jan-06 15:04:05 mst" RFC1123 = "Mon, Jan 2006 15:04:05 MST" rfc1123z = "Mon, 2006 15:04:05-0700"//RFC1123 with numeric zone RFC3339 = "2006-01-02t15:04 : 05z07:00 " Rfc3339nano =" 2006-01-02t15:04:05.999999999z07:00 " Kitchen =" 3:04pm " Stamp = "Jan _2 15:04:05" stampmilli = "Jan _2 15:04:05.000" stampmicro = "Jan _2 15:04:05.000000" Stampnano = "Jan _2 15:04:05.000000000")
These constants are some constants that are predefined for time formatting and temporal parsing, and they are all used for a specific period:
Mon Jan 2 15:04:05 MST 2006
This is Unix time 1136239445, because MST is GMT-0700, so this specified time can also be seen as
01/02 03:04:05pm ' 06-0700
Visible program apes also have a naughty side.
So all we have to do is use the time above to arbitrarily specify our own time format, for example:
Layout: = "01__02-2006 3.04.05 PM" FMT. Println (time. Now (). Format (layout))
It will output a similar time: 11__26-2014 8.40.00 PM
2. Functions
1. Time composition
Time. Duration (length of time, Time spent). Time (point) time.c (channel passage with time) (Note: Time.c:=make. Time))
2. After function
Func after (d Duration) <-chan time indicates how long it is, but does not block until the channel content is removed, and subsequent programs can continue to execute Func sleep (d Duration) to indicate how long the sleep is in a blocked state during hibernation , subsequent programs cannot be executed.
Examples illustrate the difference:
Fmt. Println ("Hello") Chan: = time. After (time. secone*1) fmt. PRINTLN ("World")
Fmt. Println ("Hello") Chan: = time. Sleep (time. secone*1) fmt. PRINTLN ("World")
The first program after the output hello, then output world, do not wait for 1s, but after 1s, chan has data, it will print out;
The second program Chan is blocked, then after the output Hello, the program becomes dormant in 1s before the world is output.
This shows that blocking and non-blocking are the essential differences between the two functions.
Because of the after feature, it is typically used to handle program timeout issues, as follows:
Select {Case m: = <-c: handle (m) case <-time. After (5 * time. Minute): FMT. Println ("Timed Out")}
3. Functions that are executed repeatedly
Func Tick (d Duration) <-chan time
Time. Tick (time. Duration) usage and time. After almost, but it is a repeating process that indicates how much time is after, and the other is consistent with after
Type Ticker //is used primarily to invoke functions or evaluate expressions for a specified time period, usually using go to open a new coprocessor, which is a staccato func newticker (d Duration) *ticker // Reborn into a ticker, this ticker contains a channel, which is sent at a given duration time. Duration d must be greater than 0func (T *ticker) Stop () //To close the corresponding Ticker, but does not close the channel
Use time control to stop ticker
Ticker: = time. Newticker (Time.millisecond *) go func () {for t: = Range Ticker. C { FMT. Println ("Tick at", T) }} () time. Sleep (Time.millisecond *) //block, the number of times the execution is sleep time/ticker time Ticker. Stop () FMT. Println ("Ticker stopped")
Stop ticker with channel control
Ticker: = time. Newticker (Time.millisecond *) c: = Make (chan int,num)//num The specified number of executions go func () {for t: = Range Ticker. C { c<-1 fmt. Println ("Tick at", T)}} () ticker. Stop ()
In this case, after executing the function num times in Ticker time, the C channel is full and the corresponding function is no longer executed.