Home [Go] How to Sleep in Golang
Post
Cancel

[Go] How to Sleep in Golang

[Go] How to Sleep in Golang


When you want to pause or delay program execution in Go, you can use the Sleep function from the time package. In this post, we will learn various ways to use sleep in Go.

Basic Usage

To use sleep in Go, import the time package and call the time.Sleep() function.

1
2
3
4
5
6
7
8
9
10
11
12
package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println("Start")
    time.Sleep(2 * time.Second)
    fmt.Println("After 2 seconds")
}

time.Sleep() takes a time.Duration type as an argument. In the example above, 2 * time.Second waits for 2 seconds.

Various Time Units

Go’s time package provides various time units:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println("Start")
    
    // Milliseconds
    time.Sleep(500 * time.Millisecond)
    fmt.Println("After 500 milliseconds")
    
    // Seconds
    time.Sleep(2 * time.Second)
    fmt.Println("After 2 seconds")
    
    // Minutes
    time.Sleep(1 * time.Minute)
    fmt.Println("After 1 minute")
    
    // Hours
    time.Sleep(1 * time.Hour)
    fmt.Println("After 1 hour")
}

Available Time Units

UnitDescription
time.NanosecondNanosecond (1ns)
time.MicrosecondMicrosecond (1µs)
time.MillisecondMillisecond (1ms)
time.SecondSecond (1s)
time.MinuteMinute (1m)
time.HourHour (1h)

Practical Examples

1. Waiting Between Repeated Tasks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main

import (
    "fmt"
    "time"
)

func main() {
    for i := 1; i <= 5; i++ {
        fmt.Printf("Executing task %d...\n", i)
        time.Sleep(1 * time.Second)
    }
    fmt.Println("All tasks completed")
}

2. Delaying Between API Calls

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package main

import (
    "fmt"
    "time"
)

func callAPI() {
    fmt.Println("Calling API...")
    // API call logic
}

func main() {
    for i := 0; i < 3; i++ {
        callAPI()
        // Wait 2 seconds between API calls (Rate limiting)
        time.Sleep(2 * time.Second)
    }
}

3. Using with Goroutines

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main

import (
    "fmt"
    "time"
)

func worker(id int) {
    for i := 0; i < 3; i++ {
        fmt.Printf("Worker %d: Task %d\n", id, i)
        time.Sleep(1 * time.Second)
    }
}

func main() {
    // Run multiple goroutines
    go worker(1)
    go worker(2)
    
    // Wait so the main goroutine doesn't exit
    time.Sleep(5 * time.Second)
    fmt.Println("All tasks completed")
}

Important Notes

  • time.Sleep() blocks the current goroutine. Other goroutines continue to run.
  • It does not guarantee exact timing. The actual wait time may vary slightly depending on the system scheduler.
  • Sleep for very short durations (nanoseconds) may have reduced accuracy.

Difference from time.After()

time.Sleep() blocks for the specified duration, while time.After() returns a channel that can be used asynchronously:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println("Start")
    
    // time.Sleep() - blocking
    time.Sleep(2 * time.Second)
    fmt.Println("After 2 seconds (Sleep)")
    
    // time.After() - non-blocking (using channel)
    <-time.After(2 * time.Second)
    fmt.Println("After 2 seconds (After)")
}

I hope this post helps you understand how to use sleep in Go!

This post is licensed under CC BY 4.0 by the author.