golang sleep 1 second code example

Example 1: golang sleep

package main

import (
    "fmt"
    "time"
)

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

    time.Sleep(2 * time.Second)

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

Example 2: sleep n seconds in go

/*
this is golang code but the
java syntax highlighting was
good, so yeah
*/
package main
import (
	"fmt"
	"time"
)

func main() {
	for i := 1; i <= 5; i++ {
    	fmt.Println(i)
        time.Sleep(1 * time.Second) // for 1 second
        time.Sleep(1 * time.Millisecond) // for 1 millisecond
    }
}

Tags:

Go Example