Sleeping in Go – How to Pause Execution

Sleeping, or waiting in Go, is part of the time package. It’s a very simple process, all you need to do is specify the duration to sleep for, which in this cases is a number followed by it’s unit.

package main

import (
    "fmt"
    "time"
)

func main() {

    fmt.Printf("Current Unix Time: %v\n", time.Now().Unix())

    fmt.Println("Sleeping...")

    time.Sleep(2 * time.Second)

    fmt.Printf("Current Unix Time: %v\n", time.Now().Unix())

}

Leave a Reply

Your email address will not be published. Required fields are marked *