Go Package time: Difference between revisions
Jump to navigation
Jump to search
(→Time) |
|||
Line 35: | Line 35: | ||
==<tt>After(Duration)</tt>== | ==<tt>After(Duration)</tt>== | ||
The <code>After()</code> returns a channel that will provide a Time instance after the given duration elapses. Equivalent with <code>NewTimer(d).C</code>. | The <code>After()</code> returns a channel that will provide a Time instance after the given duration elapses. Equivalent with <code>NewTimer(d).C</code>. | ||
=Idioms= | |||
==A Clock that Does Something Every Second== | |||
<syntaxhighlight lang='go'> | |||
for range time.Tick(1 * time.Second) { | |||
fmt.Printf(".\n") | |||
} | |||
</syntaxhighlight> |
Revision as of 22:01, 15 January 2024
External
Internal
Overview
time.After
returns a channel that after the given duration, will send current time on it. This can be used to implement a timeout withselect
.
time.Sleep()
Sleep pauses the current goroutine for at least the duration d
. A negative or zero duration causes Sleep
to return immediately:
import "time"
...
secs := 10
time.Sleep(time.Duration(secs) * time.Second)
Other available time units: time.Milliseconds
.
Now()
Return the current local time as an instance of a Time
struct.
Time
A time structure.
sec()
Return seconds since Jan 1 year 1.
UnixNano()
Return the number of nanoseconds elapsed since January 1, 1970 UTC, represented on 8 bytes (int64
). The result does not depend on the location associated with the time instance the method was called on.
After(Duration)
The After()
returns a channel that will provide a Time instance after the given duration elapses. Equivalent with NewTimer(d).C
.
Idioms
A Clock that Does Something Every Second
for range time.Tick(1 * time.Second) {
fmt.Printf(".\n")
}