Posts

Showing posts with the label Sleep

How to make a delay in Go Language

Image
The Sleep() method pauses the execution of the current thread temporarily.  Sleep()  is part of the time package. This method takes the duration as an argument.  Here is an example code that shows how to make a delay in Go Language. Here, 3*time.Second, means 3 seconds. This will sleep the current goroutine so other goroutines will continue to run. package main import ( "fmt" "time" ) func main() { // Before sleep fmt.Println( "Before sleep the time is:" , time.Now().Unix()) // pauses execution for 3 seconds time.Sleep( 3 * time.Second) // After sleep fmt.Println( "After sleep the time is:" , time.Now().Unix()) } Output : Goroutines Goroutines are methods that run concurrently with other methods. Goroutines can be thought of as lightweight threads. The cost of creating a Goroutine is tiny when compared to a thread.  Every concurrently executing activity in the Go language is known as Goroutines. Another Examp