Tickers

I was looking into timers and I came across tickers.
Basically they allow you to trigger an event at a regular time interval.
One simple example could be some kind of monitoring.

What’s interesting is that it pushes a Ticker to a channel.

Here is a basic example:

package main

import (
	"fmt"
	"os"
	"sync"
	"time"
)

func checkInBackground(ticker time.Ticker, wg *sync.WaitGroup) {
    wg.Add(1)
	for ; true; <-ticker.C {
		fmt.Println("ok")
	}
	wg.Done()
}

func termination() {
	os.Exit(1)
}

func main() {
	var wg sync.WaitGroup
	ticker := time.NewTicker(1 * time.Second)
	defer ticker.Stop()
	go checkInBackground(*ticker, &wg)
	time.AfterFunc(time.Duration(10)*time.Second, termination)
	wg.Wait()
	fmt.Println("Done as expected")
}

Not sure if it’s a nice way to achieve that as I’m learning Golang, but I did what I wanted.