Friday, 10 May 2019

Go language: Sleep goroutine for some time


“time” package provides sleep method to sleep/pause the goroutine for some time.

Example
time.Sleep(5 * time.Second)

Above statement make the main goroutine to sleep for 5 seconds. If you pass a negative or zero duration as an argument to Sleep method, then it causes Sleep to return immediately

App.go
package main

import (
 "fmt"
 "time"
)

func main() {

 fmt.Println("Going to sleep for 5 seconds")

 time.Sleep(5 * time.Second)

 fmt.Println("I woke up after 5 seconds")
}

Output
Going to sleep for 5 seconds
I woke up after 5 seconds


Previous                                                 Next                                                 Home

No comments:

Post a Comment