Go Package time: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
No edit summary
Line 7: Line 7:
=Overview=
=Overview=


* <tt>[https://golang.org/pkg/time/#After time.After]</tt> returns a channel that after the given duration, will send current time on it. This can be used to implement a timeout with <tt>[[Go Channels#select|select]]</tt>.
* <code>[https://golang.org/pkg/time/#After time.After]</code> returns a channel that after the given duration, will send current time on it. This can be used to implement a timeout with <code>[[Go Channels#select|select]]</code>.


==<tt>time.Sleep()</tt>==
==<tt>time.Sleep()</tt>==
Line 15: Line 15:
Sleep pauses the current goroutine for at least the duration d. A negative or zero duration causes Sleep to return immediately:
Sleep pauses the current goroutine for at least the duration d. A negative or zero duration causes Sleep to return immediately:


<pre>
<syntaxhighlight lang='go'>
import "time"
import "time"
...
...
Line 21: Line 21:
secs = 10
secs = 10
time.Sleep(time.Duration(secs) * time.Second)
time.Sleep(time.Duration(secs) * time.Second)
</pre>
</syntaxhighlight>


=<tt>After(Duration)</tt>=
=<tt>After(Duration)</tt>=


The <tt>After()</tt> returns a channel that will provide a Time instance after the given duration elapses. Equivalent with <tt>NewTimer(d).C</tt>.
The <tt>After()</tt> returns a channel that will provide a Time instance after the given duration elapses. Equivalent with <tt>NewTimer(d).C</tt>.

Revision as of 04:16, 23 August 2023

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 with select.

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"
...
var secs int
secs = 10
time.Sleep(time.Duration(secs) * time.Second)

After(Duration)

The After() returns a channel that will provide a Time instance after the given duration elapses. Equivalent with NewTimer(d).C.