Code with Go
Ask or search…


This Go code demonstrates the use of the time package to create a ticker and handle periodic events. Let's go through it with inline comments:
package main
import (
func main() {
// Creating a ticker 'ticker' that ticks every 500 milliseconds
ticker := time.NewTicker(500 * time.Millisecond)
// Creating a channel 'done' for signaling when to stop the ticker
done := make(chan bool)
// Launching a goroutine to handle ticker events
go func() {
for {
select {
// If a signal is received on the 'done' channel, exit the goroutine
case <-done:
// If a tick is received on the 'ticker.C' channel, print the time of the tick
case t := <-ticker.C:
fmt.Println("Tick at", t)
// Sleeping for 1600 milliseconds to allow several ticks to occur
time.Sleep(1600 * time.Millisecond)
// Stopping the ticker
// Signaling the 'done' channel to stop the goroutine
done <- true
fmt.Println("Ticker stopped")


Tick at 2024-01-23 00:19:05.3026099 +0530 IST m=+0.514576101
Tick at 2024-01-23 00:19:05.7982714 +0530 IST m=+1.010237601
Tick at 2024-01-23 00:19:06.2956784 +0530 IST m=+1.507644601
Ticker stopped
  1. 1.
    package main: Indicates that this Go file belongs to the main executable package.
  2. 2.
    import (...): Imports necessary packages, including "fmt" for formatting and printing, and "time" for handling time-related operations.
  3. 3.
    func main() { ... }: The main function, where the execution of the program begins.
  4. 4.
    ticker := time.NewTicker(500 * time.Millisecond): Creates a ticker named 'ticker' that ticks every 500 milliseconds.
  5. 5.
    done := make(chan bool): Creates a channel named 'done' for signaling when to stop the ticker.
  6. 6.
    go func() { ... }(): Launches a goroutine to handle ticker events. Inside the goroutine, it uses a select statement to wait for either a signal on the 'done' channel (indicating to stop) or a tick on the 'ticker.C' channel.
  7. 7.
    time.Sleep(1600 * time.Millisecond): Sleeps for 1600 milliseconds to allow several ticks to occur.
  8. 8.
    ticker.Stop(): Stops the ticker, preventing further ticks.
  9. 9.
    done <- true: Sends a signal to the 'done' channel, indicating that the goroutine should exit.
  10. 10.
    fmt.Println("Ticker stopped"): Prints a message indicating that the ticker has been stopped.
In summary, this code demonstrates how to use a ticker to generate periodic events and how to gracefully stop the ticker by signaling a goroutine using channels. The select statement is used to handle multiple channels in a non-blocking manner.
Last modified 1mo ago