How it works...
To execute the code with some delay only once, the Timer from the time package can be used. The concept of how this works is the same as described in the previous recipe, Running the code block periodically.
The Timer contains the C channel, which delivers the tick after a given time. After that, no other ticks are delivered through the channel.
The same functionality delivers the AfterFunc function of the time package. It just simplifies the usage. Note that there is no channel needed. The sample code uses the sync.WaitGroup to wait until the given function is executed.
The time.After is the last option in the preceding example. The function returns a channel that delivers the tick after a given period. Note the difference ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access