Timers

This Go code demonstrates the use of the time package to create and manipulate timers. Let's go through it with inline comments:

package main

import (
    "fmt"
    "time"
)

func main() {
    // Creating a timer 'timer1' that will fire after 2 seconds
    timer1 := time.NewTimer(2 * time.Second)

    // Blocking until the timer1's channel 'C' sends a value (indicating the timer has fired)
    <-timer1.C
    fmt.Println("Timer 1 fired")

    // Creating a timer 'timer2' that will fire after 1 second
    timer2 := time.NewTimer(time.Second)

    // Launching a goroutine to handle the firing of timer2
    go func() {
        // Blocking until the timer2's channel 'C' sends a value
        <-timer2.C
        fmt.Println("Timer 2 fired")
    }()

    // Stopping timer2 before it fires
    stop2 := timer2.Stop()

    // Checking if timer2 was successfully stopped
    if stop2 {
        fmt.Println("Timer 2 stopped")
    }

    // Giving some time for the goroutine to handle the stopping of timer2
    time.Sleep(2 * time.Second)
}

Output

Timer 1 fired
Timer 2 stopped

Explanation:

  1. package main: Indicates that this Go file belongs to the main executable package.

  2. import (...): Imports necessary packages, including "fmt" for formatting and printing, and "time" for handling time-related operations.

  3. func main() { ... }: The main function, where the execution of the program begins.

  4. timer1 := time.NewTimer(2 * time.Second): Creates a timer named 'timer1' that will fire after 2 seconds.

  5. <-timer1.C: Blocks until the channel 'C' of 'timer1' sends a value, indicating that the timer has fired. It then prints "Timer 1 fired."

  6. timer2 := time.NewTimer(time.Second): Creates a timer named 'timer2' that will fire after 1 second.

  7. go func() { ... }(): Launches a goroutine to handle the firing of 'timer2'. Inside the goroutine, it blocks until the channel 'C' of 'timer2' sends a value, then it prints "Timer 2 fired."

  8. stop2 := timer2.Stop(): Attempts to stop 'timer2' before it fires. The Stop method returns a boolean value indicating whether the timer was successfully stopped.

  9. if stop2 { fmt.Println("Timer 2 stopped") }: Checks if 'timer2' was successfully stopped and prints a message if it was.

  10. time.Sleep(2 * time.Second): Gives some time for the goroutine to handle the stopping of 'timer2' before the program exits.

In summary, this code demonstrates how to use the time package to create timers, block until a timer fires, and stop a timer before it fires. The use of goroutines allows concurrent handling of timer events.

Last updated