DEV Community

Nelson Figueroa
Nelson Figueroa

Posted on • Originally published at nelson.cloud on

Using time.Sleep() in Go

Basic Usage of time.Sleep()

You can use time.Sleep() to pause your program for a predetermined amount of time, similar to most programming languages.

First, you should know that the time package has useful constants that allow you to conveniently specify time in units.

const (
    Nanosecond Duration = 1
    Microsecond = 1000 * Nanosecond
    Millisecond = 1000 * Microsecond
    Second = 1000 * Millisecond
    Minute = 60 * Second
    Hour = 60 * Minute
)
Enter fullscreen mode Exit fullscreen mode

They’re accessed with the time.<Constant> notation. (e.g. time.Second)

You can use these constants with the time.Sleep() function. For example, if we want to pause execution for 1 second, we can write the following code:

package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println("one second will pass between this message...")
    time.Sleep(time.Second)
    fmt.Println("...and this message")
}
Enter fullscreen mode Exit fullscreen mode

We can do some multiplication with time.Second to pause a program for 30 seconds:

package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println("30 seconds will pass between this message...")
    time.Sleep(time.Second * 30)
    fmt.Println("...and this message")
}
Enter fullscreen mode Exit fullscreen mode

You can do multiplication with all of the other constants mentioned as well.

Examples

Here are some more examples using the other constants.

Sleep for 500 milliseconds

package main

import (
    "time"
)

func main() {
    time.Sleep(time.Millisecond * 500)
}
Enter fullscreen mode Exit fullscreen mode

Sleep for 10 seconds

package main

import (
    "time"
)

func main() {
    time.Sleep(time.Second * 10)
}
Enter fullscreen mode Exit fullscreen mode

Sleep for 5 minutes

package main

import (
    "time"
)

func main() {
    time.Sleep(time.Minute * 5)
}
Enter fullscreen mode Exit fullscreen mode

Sleep for 2 hours

Not really sure why you would use this, but just know it’s possible.

package main

import (
    "time"
)

func main() {
    time.Sleep(time.Hour * 2)
}
Enter fullscreen mode Exit fullscreen mode

References

My main motivation for writing this is that I think the official documentation is way too dense and doesn’t show several examples of time.Sleep(). I just needed some examples to understand the syntax and move on with my day.

Top comments (0)