This repository demonstrates how to reset a ticker in Go, allowing dynamic changes to the ticker's interval. Tickers trigger actions at regular intervals, and resetting a ticker adjusts the interval while keeping the ticker running.
- This example covers how to reset a ticker to dynamically change the interval during program execution.
- It demonstrates starting a ticker, resetting the ticker's interval, and stopping the ticker after a specified time.
package main
import (
"fmt"
"time"
)
func main() {
// Resetting a Ticker
// Resetting a ticker allows you to change the ticker's interval dynamically
// Create a ticker that ticks every 1 second
ticker := time.NewTicker(1 * time.Second)
go func() {
for t := range ticker.C {
fmt.Println("Tick at", t)
}
}()
time.Sleep(3 * time.Second)
// Reset the ticker to tick every 2 seconds instead of 1 second
ticker.Reset(2 * time.Second)
time.Sleep(6 * time.Second)
ticker.Stop()
fmt.Println("Ticker stopped.")
// The ticker initially ticks every 1 second.
// After 3 seconds, ticker.Reset(2 * time.Second) changes the interval to 2 seconds
// The ticker continues ticking at the new interval until it is stopped
}
- Make sure you have Go installed. If not, you can download it from here.
- Clone this repository:
git clone https://github.com/Rapter1990/go_sample_examples.git
- Navigate to the
021_tickers/04_reset_ticker
directory:
cd go_sample_examples/021_tickers/04_reset_ticker
- Run the Go program:
go run main.go
When you run the program, you should see the following output:
Tick at 2024-09-14 11:45:58.073901 +0300 +03 m=+1.000614901
Tick at 2024-09-14 11:45:59.073901 +0300 +03 m=+2.000614901
Tick at 2024-09-14 11:46:00.073901 +0300 +03 m=+3.000614901
Tick at 2024-09-14 11:46:02.0745411 +0300 +03 m=+5.001255001
Tick at 2024-09-14 11:46:04.0745411 +0300 +03 m=+7.001255001
Tick at 2024-09-14 11:46:06.0745411 +0300 +03 m=+9.001255001
Ticker stopped.