Cookies management by TermsFeed Cookie Consent

😴 Sleep function in Go - pause the program execution

Last updated:
introduction time

To pause the execution of a current program in Go, delay code execution, and wait for a specified period of time, you just need to use the Sleep() function defined in the time package. As an argument, this function takes a variable of type time.Duration, which is the amount of time the program execution should be stopped for. It can be expressed as a number multiplied by a unit constant. For example 3*time.Second means that the execution will be stopped for 3 seconds. Available units are:

package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println("before Sleep()")

    time.Sleep(3 * time.Second)

    fmt.Println("waking up after Sleep()")
}

Thank you for being on our site 😊. If you like our tutorials and examples, please consider supporting us with a cup of coffee and we'll turn it into more great Go examples.

Have a great day!

Buy Me A Coffee

⏲️ Measure execution time in Go

Learn how to measure the time taken by a function
introduction time

📝 Convert date or time to string in Go

shorts introduction time

✨ 5 different ways to loop over a time.Ticker in Go

Learn how to use the popular time.Ticker struct in loops
introduction time