Skip to content

Latest commit

 

History

History

002_stop_timer

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Go Sample Example - Stopping a Timer

This repository demonstrates how to stop a timer in Go before it fires. It includes an example of creating a timer that can be stopped, preventing it from executing the intended action after a specified duration.

📖 Information

  • This example shows how to stop a Go timer using the Stop method before it fires.
  • If the timer is successfully stopped, the program will prevent the timer's action from executing, and the message indicating the timer has fired will not be printed.

💻 Code Example

package main

import (
	"fmt"
	"time"
)

func main() {

	// Stopping a Timer
	// You can stop a timer before it fires using the Stop method.
	// If the timer is stopped before it fires, the program will not receive any value from the timer's channel

	// Create a timer that will fire after 3 seconds
	timer := time.NewTimer(3 * time.Second)

	// Start a goroutine to stop the timer after 1 second
	go func() {
		time.Sleep(1 * time.Second)
		stop := timer.Stop()
		if stop {
			fmt.Println("Timer stopped before it fired.")
		}
	}()

	// Wait for the timer to fire
	<-timer.C
	fmt.Println("This will not be printed if the timer is stopped.")
}

🏃 How to Run

  1. Make sure you have Go installed. If not, you can download it from here.

  2. Clone this repository:

    git clone https://github.com/Rapter1990/go_sample_examples.git
  3. Navigate to the 001_stopping_timer directory:

    cd go_sample_examples/020_timers/001_stopping_timer
  4. Run the Go program:

    go run 001_stopping_timer.go

📦 Output

When you run the program, you should see the following output:

Timer stopped before it fired.

If the timer is not stopped in time, the program will print:

Timer stopped before it fired.
fatal error: all goroutines are asleep - deadlock!