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 (
"fmt"
"time"
)
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:
return
// 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
ticker.Stop()
// 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
Explanation:
-
package main
: Indicates that this Go file belongs to the main executable package. -
import (...)
: Imports necessary packages, including "fmt" for formatting and printing, and "time" for handling time-related operations. -
func main() { ... }
: The main function, where the execution of the program begins. -
ticker := time.NewTicker(500 * time.Millisecond)
: Creates a ticker named 'ticker' that ticks every 500 milliseconds. -
done := make(chan bool)
: Creates a channel named 'done' for signaling when to stop the ticker. -
go func() { ... }()
: Launches a goroutine to handle ticker events. Inside the goroutine, it uses aselect
statement to wait for either a signal on the 'done' channel (indicating to stop) or a tick on the 'ticker.C' channel. -
time.Sleep(1600 * time.Millisecond)
: Sleeps for 1600 milliseconds to allow several ticks to occur. -
ticker.Stop()
: Stops the ticker, preventing further ticks. -
done <- true
: Sends a signal to the 'done' channel, indicating that the goroutine should exit. -
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.