Table of contents
1.
Introduction
2.
Tickers
2.1.
Example
3.
Epoch
3.1.
Example
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Go Tickers & Epoch

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we are looking at what are tickers & epochs in Go and how we can use tickers & epochs effectively within your own Go applications.

Tickers

When we wish to do some work at regular intervals, we use Go Tickers. The Stop() method can be used to stop tickers in the same way as it can stop timers.

The NewTicker() method creates a new Ticker with a channel that delivers the time based on the duration input. The time limit must be greater than zero. The Ticker will panic if this does not happen.

Tick() is a wrapper for NewTicker, which allows you to access the ticking channel. Clients who do not want to turn off the Ticker can use the Tick() method.

Example

package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("Go Tickers ")
// this creates a new ticker which will
    // `tick` every 1 second.
    ticker := time.NewTicker(1 * time.Second)

    // for every `tick` that our `ticker`
    // emits, we print `tock`
for _ = range ticker.C {
fmt.Println("tock")
}
}
You can also try this code with Online C Compiler
Run Code

Output

Go Tickers
Tock
Tock
^Csignal: interrupt
You can also try this code with Online C Compiler
Run Code

This example for showing how we can use tickers. 

Epoch

In Go, We can produce the time in seconds, milliseconds, nanoseconds. The time package has the required methods like secs, Nanos, etc., which help us.

The reference time is the Unix epoch. We may also convert nanoseconds or milliseconds into time format.

Example

package main  
import "fmt"  
import "time"  
func main(){  
    p := fmt.Println  
    current_time := time.Now()  
    secs := current_time.Unix()  
    nanos := current_time.UnixNano()  
    fmt.Println(current_time)  
    millis := nanos / 1000000  
    p(secs)  
    p(millis)  
    p(nanos)  
    p(time.Unix(secs, 0))  
    p(time.Unix(0, nanos))  
}  

Output:

2022-03-21 08:50:41.601076493 +0000 UTC m=+0.000061970
1647852641
1647852641601
1647852641601076493
2022-03-21 08:50:41 +0000 UTC
2022-03-21 08:50:41.601076493 +0000 UTC
You can also try this code with Online C Compiler
Run Code

FAQs

  1. Difference between Timer and Tickers? 
    Ans: Timers are for when you want to do something once in the future - tickers are when you want to do something repeatedly at regular intervals.
     
  2. What can we do if we had a task that we wanted to run in the background?
    Ans: If we had a task that we wanted to run in the background, we could move ours for a loop that iterates over Ticker.C to inside a goroutine, allowing our application to execute other tasks.
     
  3. What is Go Epoch? 
    Ans: We can produce the time in seconds, milliseconds, nanoseconds.This is called Epoch. The time package has the required methods like secs, Nanos, etc., which help us.

Key Takeaways

In this article, we have extensively discussed Go Tickers & Epoch. We've looked at how to utilize Tickers & Epoch before repeated activities in Go applications, both on the main thread and as a background process.

We hope that this blog enhances your knowledge regarding Go Tickers & Epoch, and if you would like to learn more, check out our articles on Library. If you wish to learn about Inode in UNIX, you can visit INODE. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass