Introduction to the time.Timer package
time.Timer
is a package that deals with times or events that will occur when the process is executed. For example, we will provide a ‘delay’ for a few seconds to go to the next process or there is another scheme.
Why should we use time.Timer?
time.Timer
is often used when we want our program process to be executed in several processes or only one process at a certain time or at a certain time only or at the next time within a certain time period, so that we can schedule the next process to be paused with a certain time, for example 1 second.
Method Usage
There are several methods that we can use, including:
time.NewTimer()
is used to initialize the processdelay
with certain units, usually we can addtime.Second
time.After()
is used without initialization so it is more concise and is processed to delay after a certain time has been determinedtime.AfterFunc()
is used almost the same astime.After()
but what is different is that the next function is sent as a param which will later be executed
Implementation and samples of time.NewTimer
In the first sample and implementation, we will print the process with a delay of 5 seconds. Here’s an example of the code.
func main() {
timer := time.NewTimer(5 * time.Second)
fmt.Println(time.Now())
time := <-timer.C
fmt.Println(time)
}
In the code above, we initialize it using time.New(5*time.Second)
. This means we will create a timer with delay
or pause for 5 seconds so that when we print in the next process we will see the difference between the initial print and the print the end time for 5 seconds. Below are the results if we execute the program.
✗ go run app.go
2023-07-14 19:31:12.30003 +0700 WIB m=+0.000059626
2023-07-14 19:31:17.300921 +0700 WIB m=+5.001097376
Apart from that, we can also use time.After
to delay
the process in the program. This is even shorter and easier to implement. For example, like the code below:
func main() {
fmt.Println(time.Now())
channel := time.After(5 * time.Second)
tick := <-channel
fmt.Println(tick)
}
The result will be the same as using time.NewTimer
✗ go run app.go
2023-07-14 19:43:02.209571 +0700 WIB m=+0.000136293
2023-07-14 19:43:07.211192 +0700 WIB m=+5.00188
And finally, we can also use the time.AfterFunc()
method, which is the difference, namely doing a delay
but after that we are sent what function will be executed after we add delay
. Here’s an example of the code
func main() {
group := sync.WaitGroup{}
group.Add(1)
time.AfterFunc(5*time.Second, func() {
fmt.Println(time.Now())
fmt.Println("Execute after 5 seconds")
group.Done()
})
fmt.Println(time.Now())
group.Wait()
}
The results of the process in the program above are as follows.
✗ go run app.go
2023-07-14 19:48:25.971889 +0700 WIB m=+0.000073292
2023-07-14 19:48:30.972922 +0700 WIB m=+5.001231959
Execute after 5 seconds
Conclusion
So we can learn that the time
package actually has a lot of methods that we usually need, especially when we want to delay
a process that can provide a pause that we have previously determined.