Cookies management by TermsFeed Cookie Consent
Russia has invaded Ukraine and already killed tens of thousands of civilians, with many more raped or tortured. It's a genocide. We need your help. Let's fight back against the Russian regime.
Help Ukraine! Fight the Russian regime!

😴 Sleep function in Go - pause the program execution

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

⏲️ 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