Go Package time: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 17: Line 17:
import "time"
import "time"
...
...
var secs int
secs := 10
secs = 10
time.Sleep(time.Duration(secs) * time.Second)
time.Sleep(time.Duration(secs) * time.Second)
</syntaxhighlight>
</syntaxhighlight>
Other available time units: <code>time.Milliseconds</code>.


=<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>.

Revision as of 02:33, 1 September 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()

https://golang.org/pkg/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.

After(Duration)

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