Golang Code

Sleeping in Go – How to Pause Execution

· 71 words · 1 minute read #seconds #sleep #sleeping #time #unix #wait

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 case is a number followed by it’s unit, so 2 * time.Second means 2 seconds.

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())

}
Image of Author Edd Turtle

Author: Edd Turtle

Edd is the Lead Developer at Hoowla, a prop-tech startup, where he spends much of his time working on production-ready Go and PHP code.

See something which isn't right? You can contribute to this page on GitHub or just let us know in the comments below. Thanks for reading!