time.Sleep() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time.
The Sleep() function in Go language is used to stop the latest go-routine for at least the stated duration d. And a negative or zero duration of sleep will cause this method to return instantly. Moreover, this function is defined under the time package. Here, you need to import the “time” package to use these functions.
func Sleep(d Duration)
Here, d is the duration of time in seconds to sleep.
Return Value: It pauses the latest go-routine for the stated duration then returns the output of any operation after the sleep is over.
Here, after running the above code when the main function is called then due to Sleep method the stated operation is stopped for the given duration then the result is printed.
Here, in the above code “output1” is printed as the duration of timeout(in After() method) is greater than the sleep time(in Sleep() method) so, the output is printed before the timeout is displayed but after that, the below case has timeout duration less than the sleep time so, before printing the output the timeout is displayed hence, “timeout….2” is printed.