go Pause execution for several seconds
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("Before sleeping")
time.Sleep(5 * time.Second)
fmt.Println("After sleeping")
}
This Go program demonstrates the usage of the time.Sleep()
function to pause execution for a specified duration.
Import necessary packages: The
fmt
package is used for formatted I/O, and thetime
package provides functionality related to time.Define the
main()
function as the entry point of the program.Print "Before sleeping" using
fmt.Println()
to indicate the start of the sleep period.Use
time.Sleep()
to pause the program execution.5 * time.Second
specifies the duration for which the program will sleep (in this case, 5 seconds).Print "After sleeping" using
fmt.Println()
after the sleep duration elapses to indicate the end of the sleep period.