Ad space (banner)
๐ŸนGo Lessons
Lesson 56 / 68

Waiting for a Fixed Amount of Time (time.Sleep)

In this lesson you will learn how to pause for a set time with time.Sleep in Go, covering the basics of programs that deal with time. This is for anyone searching for "Go time.Sleep how to use".

time.Sleep() pauses processing for the time you specify. Use it to space out calls to an API, or to slow a process down deliberately.

The sample code prints "Starting", waits one second with time.Sleep(1 * time.Second), and then prints "Ran after 1 second". Notice how it is written with a duration constant such as time.Second.

A common stumbling block for beginners is over-using Sleep and making the whole program feel unresponsive. Go is a language built for concurrency (goroutines), and time.Sleep appears frequently when coordinating the timing of concurrent work.

In real-world development time.After combined with goroutines is the more flexible choice for timeouts in real applications.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Go directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)