The Wayback Machine - https://web.archive.org/web/20240829143053/https://www.geeksforgeeks.org/time-sleep-function-in-golang-with-examples/
Open In App

time.Sleep() Function in Golang With Examples

Last Updated : 20 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

Syntax:

func Sleep(d Duration)

Here, d is the duration of time 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.

Example 1:





Output:

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

Example 2:





Output:

output1
timeout....2

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.



Similar Reads

time.Time.Year() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Year() function in Go language is used to find the year in which the specified "t" happens. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functions. Syntax: func (t Tim
2 min read
time.Time.Hour() Function in Golang With Examples
The Time.Hour() function in Go language is used to check the hour within the day in which the stated "t" presents itself in the range [0, 23]. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functions. Syntax: func (t Time) Hour() int Here, "t" is the stated time. Return V
2 min read
time.Time.Day() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Day() function in Go language is used to check the day of the month in which the stated "t" presents itself. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functions. Sy
2 min read
time.Time.Add() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Add() function in Go language is used to add the stated time and duration. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functions. Syntax: func (t Time) Add(d Duration
2 min read
time.Time.AddDate() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.AddDate() function in Go language is used to check the time which is equivalent to adding the stated number of years, months, and days to the given "t". For example, if the parameters of the method are like (-2, 4, 5) and the stated t is February
3 min read
time.Time.After() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.After() function in Go language is used to check if the stated time instant t is after the stated u or not. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functions. Syn
2 min read
time.Time.Date() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Date() function in Go language is used to check the year, month, and day in which the stated "t" presents itself. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these function
2 min read
time.Time.AppendFormat() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.AppendFormat() function in Go language is like Format() method but this method also appends the stated textual representation to the stated "b". Moreover, this function is defined under the time package. Here, you need to import the "time" package
2 min read
time.Time.Before() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Before() function in Go language is used to check if the stated time instant t is before the stated u or not. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functions. S
2 min read
time.Time.Clock() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Clock() function in Go language is used to check the hour, minute, and second within the day in which the stated "t" presents itself. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order t
2 min read
time.Time.Equal() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Equal() function in Go language is used to check if the stated times "t" and "u" represents identical time instant or not. And two times located in different locations can even be equal. Moreover, this function is defined under the time package. H
2 min read
time.Time.IsZero() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.IsZero() function in Go language is used to check if the stated time "t" implies zero time instant or not i.e, January 1, year 1, 00:00:00 UTC. Moreover, this function is defined under the time package. Here, you need to import "time" package in o
2 min read
time.Time.Local() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Local() function in Go language is used to find "t" with the location that is set to local time. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functions. Syntax: func (
2 min read
time.Time.Location() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Location() function in Go language is used to check the data of the time zone that is associated with "t". Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functions. Synt
2 min read
time.Time.Minute() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Minute() function in Go language is used to find the minute offset within the stated hour that is provided by "t" and the range is [0, 59]. Moreover, this function is defined under the time package. Here, you need to import the "time" package in o
2 min read
time.Time.Month() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Month() function in Go language is used to find the month of the year as provided by t. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functions. Syntax: func (t Time) M
2 min read
time.Time.Nanosecond() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Nanosecond() function in Go language is used to find the nanosecond offset within the second as provided by "t" and the range is [0, 999999999]. Moreover, this function is defined under the time package. Here, you need to import "time" package in
2 min read
time.Time.Second() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Second() function in Go language is used to find the second offset within the minute as provided by "t" and the range is [0, 59]. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to us
2 min read
time.Time.String() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.String() function in Go language is used to yield the time which is formatted with the help of the format string. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these function
2 min read
time.Time.Sub() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Sub() function in Go language is used to yield the duration obtained by performing the operation t-u. And if the output here surpasses the maximum or the minimum value that can be stored in a Duration "d" then the maximum or the minimum duration w
2 min read
time.Time.UTC() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.UTC() function in Go language is used to yield "t" with the location that is set to UTC. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functions. Syntax: func (t Time)
2 min read
time.Time.Unix() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Unix() function in Go language is used to yield "t" as a Unix time that is the number of seconds passed from January 1, 1970, in UTC and the output here doesn't rely upon the location connected with t. Moreover, this function is defined under the
2 min read
time.Time.UnixNano() Function in Golang with Examples
In Go language, time packages supply functionality for determining as well as viewing time. The Time.UnixNano() function in Go language is used to yield "t" as a Unix time that is the number of seconds passed from January 1, 1970, in UTC and the output here doesn't rely upon the location connected with t. Moreover, this function is defined under th
2 min read
time.Time.YearDay() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.YearDay() function in Go language is used to find the day of the year as provided by "t". Where, the range for non-leap years is [1, 365] and for leap years it is [1, 366]. Moreover, this function is defined under the time package. Here, you need
2 min read
time.Time.Zone() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Zone() function in Go language is used to determine the time zone that is in work at time "t". Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functions. Syntax: func (t
2 min read
time.Time.GobEncode() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The GobEncode() function in Go language is used to implement the gob.GobEncoder interface. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functions. Syntax: func (t Time) GobEnco
2 min read
time.Time.ISOWeek() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The ISOWeek() function in Go language is used to find the ISO 8601 year and week number in which the stated "t" happened. Where the range of the week is from 1 to 53. Moreover, this function is defined under the time package. Here, you need to import the "
2 min read
time.Time.In() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The In() function in Go language is used to find a copy of the stated "t" that represents the identical time instant, but along with the copy's location data that is set to "loc" for display uses. And if "loc" is nil then panic is returned. Moreover, this
2 min read
time.Time.MarshalBinary() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The MarshalBinary() function in Go language is used to implement the encoding.BinaryMarshaler interface. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functions. Syntax: func (t
2 min read
time.Time.GobDecode() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The GobDecode() function in Go language is used to implement the gob.GobDecoder interface. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functions. Syntax: func (t *Time) GobDec
2 min read
Article Tags :