🌐
Go by Example
gobyexample.com › tickers
Go by Example: Tickers
Timers are for when you want to do something once in the future - tickers are for when you want to do something repeatedly at regular intervals. Here’s an example of a ticker that ticks periodically until we stop it · Tickers use a similar mechanism to timers: a channel that is sent values.
🌐
Go
go.dev › src › time › tick.go
chan Time - // The channel on which the ticks are delivered.
16 type Ticker struct { 17 C <-chan Time // The channel on which the ticks are delivered. 18 initTicker bool 19 } 20 21 // NewTicker returns a new [Ticker] containing a channel that will send 22 // the current time on the channel after each tick. The period of the 23 // ticks is specified by the duration argument.
Discussions

proposal: time: add TickerChan method that returns Ticker.C
Proposal Details Background The Ticker struct currently have exporter C channel which is used to send ticks to. type Ticker struct { C More on github.com
🌐 github.com
10
September 24, 2024
Tickers on gobyexample
I think in this case it wouldn’t matter but in general you should take care of any goroutines before leaving the routine-creating routine. Let’s say main was a function called many times. It would leak goroutines without the done chan. Explanation if this isn’t already clear to you More on reddit.com
🌐 r/golang
9
7
October 23, 2023
How to unit test this goroutine select that uses time.Ticker
Instead of taking a time.Ticker, take in an interface for one, then you can have complete control over the ticker. Where does the ctx come from in the ctx.Done case? Right now it appears to be global which is going to hinder your testing. Pass it in as another parameter which then lets you isolate the context to just this function More on reddit.com
🌐 r/golang
25
18
December 27, 2023
go - Does a ticker tell a goroutine it is stopped through its ticker.C? - Stack Overflow
I thought when I call ticker.Stop(), ticker.C should tell the goroutine that it is over, so the for loop should end, but it doesn't look like that, the string "ticker stopped" is never printed. More on stackoverflow.com
🌐 stackoverflow.com
October 16, 2017
🌐
Medium
leapcell.medium.com › working-with-scheduled-tasks-in-go-timer-and-ticker-5b6c4289a63c
Working with Scheduled Tasks in Go: Timer and Ticker | by Leapcell | Medium
June 29, 2025 - We are Leapcell, your top choice for hosting Go projects. ... In daily development, we may encounter situations where we need to delay the execution of some tasks or execute them periodically. At this point, we need to use timers in Go. In Go, there are two types of timers: time.Timer (one-shot timer) and time.Ticker (periodic timer).
🌐
GitHub
github.com › golang › go › issues › 69611
proposal: time: add TickerChan method that returns Ticker.C · Issue #69611 · golang/go
September 24, 2024 - type Ticker struct { C <-chan Time // The channel on which the ticks are delivered.
Author   golang
🌐
Leapcell
leapcell.io › blog › understanding-golang-ticker-a-guide-to-timed-operations
Understanding Golang Ticker: A Guide to Timed Operations | Leapcell
July 25, 2025 - Golang's `time.Ticker` schedules periodic tasks and requires proper stopping to manage resources.
🌐
gosamples
gosamples.dev › tutorials › 5 different ways to loop over a time. ticker in go
✨ 5 different ways to loop over a time.Ticker in Go
February 27, 2023 - When you want to perform some task every fixed period of time, use the standard for loop over the C channel of time.Ticker. Such a loop allows you to schedule cron-like tasks or worker functions, where some code executes every specified time. Check out the example and the output.
🌐
Antoine-augusti
blog.antoine-augusti.fr › 2018 › 03 › golang-instant-first-tick-for-ticker
Golang : instant first tick for ticker – Antoine Augusti
package main import "time" import "fmt" func main() { ticker := time.NewTicker(500 * time.Millisecond) go func() { for t := range ticker.C { fmt.Println("Tick at", t) } }() time.Sleep(1600 * time.Millisecond) ticker.Stop() fmt.Println("Ticker stopped") }
Find elsewhere
🌐
DEV Community
dev.to › ankitmalikg › go-ticker-vs-timer-4glb
Go: Ticker vs Timer - DEV Community
March 11, 2023 - In this example, we create a new ticker using the time.NewTicker function and pass it an interval of 500 milliseconds. We then create a goroutine that loops over the ticker's C channel, printing the current time each time the ticker fires.
🌐
TutorialEdge.net
tutorialedge.net › home › golang development › go tickers tutorial
Go Tickers Tutorial | TutorialEdge.net
May 2, 2019 - Now, when we go to run this, our Go application will run indefinitely until we ctrl-c quit the program and every 1 second it will print out tock to the terminal. ... So we have been able to implement a really simple Go application that uses a ticker to repeatedly perform an action.
🌐
GitHub
github.com › golang › go › issues › 69601
proposal: time/tick.go: Add `C()` method to expose the `Ticker` Channel · Issue #69601 · golang/go
September 23, 2024 - type Ticker struct { C <-chan Time // The channel on which the ticks are delivered.
Author   golang
🌐
TutorialsPoint
tutorialspoint.com › how-to-use-tickers-in-golang
How to use Tickers in Golang?
November 1, 2021 - We can use them with goroutines as well so that we can run these tasks in the background of our application without breaking the flow of the application. The function that we use in tickers is the NewTicker() function which takes time as an argument and we can supply seconds and even milliseconds in it.
🌐
YourBasic
yourbasic.org › golang › time-reset-wait-stop-timeout-cancel-interval
Timer and Ticker: events in the future · YourBasic Go
time.Tick returns a channel that delivers clock ticks at even intervals: go func() { for now := range time.Tick(time.Minute) { fmt.Println(now, statusUpdate()) } }() The underlying time.Ticker will not be recovered by the garbage collector.
🌐
Reddit
reddit.com › r/golang › how to unit test this goroutine select that uses time.ticker
r/golang on Reddit: How to unit test this goroutine select that uses time.Ticker
December 27, 2023 -

I'm not familiar with goroutines and am struggling to unit test the snippet below.

Requirement

It's some kind of data generator. It ticks every minute, generates some data and publishes it to Kafka.

func (i impl) someName(ctx context.Context, t *time.Ticker) error {
        //ticker ticks every 5 seconds

    defer t.Stop()

    for {
	select {
	        case timer := <-t.C:
		fmt.Println("timer is called....")
                        // perform some logic e.g. publish a message to Kafka
		case <-ctx.Done():
		t.Stop()
		return nil
	}
    }
}

Problem

In the unit test, when someNameis called it gets stuck looping in the first case statement.. and I'm unable to assert.NoErrorafter publishing to Kafka successfully.

Questions:

  1. Is there anything wrong with the way that the goroutine is written given the requirements? If yes, what should be the idiomatic approach?

  2. How should the unit test be written such that I can test the first case deterministically and only once?

Any help is appreciated. I've spent 2 days cracking this to no avail.

Thanks gophers!

🌐
GitHub
github.com › golang › go › issues › 2650
Feature request: time.Ticker.Stop should close 'C' channel as well. · Issue #2650 · golang/go
January 3, 2012 - Simple use case: ticker := time.NewTicker(1 * time.Seconds) for _ = range ticker.C { if somethingHappened() { ticker.Stop() } } We can't resume the ticker after it was stopped anyway, why keeping the channel in an opened state? That's a ...
Author   golang
🌐
Medium
shivamsouravjha.medium.com › golang-how-to-use-ticker-and-timer-dde84644bb46
How to use Ticker in Golang | Medium
August 18, 2024 - We can put a ticker and after a gap of every 30 seconds hit on a endpoint of our application. Like other data structures in Golang, ticker is also initialised via a simple definition.