timer is a single event in the future. A timer can make the process wait a specified time. When creating the timer, you set the time to wait.
To make a timer expire after 3 seconds, you can use
time.NewTimer(3 * time.Second). If you only want to wait, use
If you want to repeat an event, use
Related course: Introducing Go: Build Reliable, Scalable Programs
This program waits for 3 seconds before continuing. The line
<- t1.C blocks the timers channel
C. It unblocks when the timer has expired.
timer can be stopped. In some scenarios you want to be able to cancel a process, like if you download a file or if you are trying to connect.
The program below allows a user to cancel the timer.