What is time.Sleep() Function in Golang

Golang time.Sleep() is a built-in function that is used to pause the execution of a program for a specified duration. The Sleep() function takes a sleep duration as a single argument and is of type time.Duration.

The time.Duration is a type alias for int64; its value is the number of nanoseconds. A negative or zero sleep duration will cause this method to return instantly.

Syntax

func Sleep(dur Duration)

Parameters

The dur is the duration of time in seconds to sleep.

Return value

The time.Sleep() function pauses the latest go-routine for the stated duration and returns the operation’s output after sleep.

Important note

The time.Sleep() function blocks the current Goroutine and will not allow any other Goroutines to run while it’s sleeping.

This can induce a performance issue if the Goroutine is sleeping for a long time.

Use the time to perform some action periodically and don’t want to block other Goroutines.Ticker() method instead.

Example

We must import the time package to use the Sleep() function. 

Write a Golang program that will put the program to sleep for 5 seconds.

package main

import (
  "fmt"
  "time"
)

func main() {
  fmt.Println("Started")
  time.Sleep(5 * time.Second)
  fmt.Println("Finished")
}

Output

Started
// 5 seconds sleep
Finished

In this example, the program will print “Started” and wait 5 seconds before printing “Finished”.

Note that the duration argument is for time.Sleep can be expressed using different time units, including time.Second, time.Minute, time.Hour, etc.

  1. To tell the sleep function to sleep for 10 seconds in Go, use this syntax: time.Sleep(10 * time.Second).
  2. To sleep a program for 5 seconds in Golang, use this syntax: time.Sleep(5 * time.Second).
  3. To sleep a program for 1 second in Go, use this syntax: time.Sleep(1 * time.Second).

Create a timer using the sleep() function in Go

We will create a timer that counts down from 5.

package main

import (
  "fmt"
  "time"
)

func timer(t int) {
  for {
    if t <= 0 {
      break
    } else {
      fmt.Println(t)
      time.Sleep(1 * time.Second)
      t--
     }
  }
}

func main() {
  timer(5)
}

Output

5
4
3
2
1

The timer() is a custom function that takes an integer t as an argument and loops until t is less than or equal to 0.

On each iteration of the loop, the value of t is printed, and the function waits for one second using the time.Sleep() function.

Finally, the t value is decremented. This continues until t is 0, at which point the loop terminates.

In the main function, the timer function is called with argument 5.

Key takeaways of time.Sleep() function

  1. The time.Sleep() function from the Go standard library is used to pause the execution of a program for a specified amount of time.
  2. The time.Sleep() function accepts a single argument of type time.Duration is the amount of time to sleep.
  3. The time.Duration type is an int64 value representing the number of nanoseconds.
  4. The time.Sleep() function is a blocking operation and will halt the program’s execution for a specified amount of time.
  5. The time.Sleep() function is generally used for performance testing, waiting for resources to become available, or adding delays between operations.
  6. To convert seconds to time.Duration, use time.Second, time.Minute, or time.Hour, for example, 1 * time.Second, 5 * time.Minute, or 1 * time.Hour.
  7. When using time.Sleep(), be cautious of sleeping for too long as it can slow down the performance of your program, and also be cognizant of sleeping for too little as it may not be enough time to complete the desired operation.

Conclusion

Golang Sleep() is a useful function for stopping the current program for a given time. It is an important part of program flow control and is frequently utilized in applications that need timed events or delays.

Leave a Comment