Cookies management by TermsFeed Cookie Consent

😴 Sleep function in Go - pause the program execution

Last updated:
introduction time

Please consider supporting us by disabling your ad blocker

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 (

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

πŸ“… YYYY-MM-DD date format in Go

Learn how to format date without time
introduction time

♾️ Infinite loop in Go

Learn how to define a "while true" loop
introduction loop