Basic Usage of time.Sleep()
You can use time.Sleep() to pause your program for a predetermined amount of time, similar to most programming languages.
First, you should know that the time package has useful constants that allow you to conveniently specify time in units.
| |
They’re accessed with the time.<Constant> notation. (e.g. time.Second)
You can use these constants with the time.Sleep() function. For example, if we want to pause execution for 1 second, we can write the following code:
| |
We can do some multiplication with time.Second to pause a program for 30 seconds:
| |
You can do multiplication with all of the other constants mentioned as well.
Examples
Here are some more examples using the other constants.
Sleep for 500 milliseconds
| |
Sleep for 10 seconds
| |
Sleep for 5 minutes
| |
Sleep for 2 hours
Not really sure why you would use this, but just know it’s possible.
| |
References
My main motivation for writing this is that I think the official documentation is way too dense and doesn’t show several examples of time.Sleep(). I just needed some examples to understand the syntax and move on with my day.