O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required