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

The timeout for the long-running operation in the previous code is implemented with the use of the time.After function, which provides the channel delivering the tick after the given period.

The operation itself is wrapped to select a statement which chooses between the time.After channel and the default option, which executes the operation.

Note that you need to allow the code to read from the time.After channel periodically to find out whether the timeout is exceeded or not. Otherwise, if the default code branch blocks the execution entirely, there is no way how to find out if the timeout has already elapsed. 

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