Signals

The provided Go code demonstrates how to handle signals such as SIGINT and SIGTERM in a Go program using channels and goroutines. Here's a breakdown of the code:

package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"
)

func main() {
	// Create a channel to receive signals.
	sigs := make(chan os.Signal, 1)

	// Notify the program to listen for SIGINT and SIGTERM signals.
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

	// Create a channel to signal when the program is done.
	done := make(chan bool, 1)

	// Start a goroutine to handle signals.
	go func() {
		// Wait for a signal to be received on the 'sigs' channel.
		sig := <-sigs
		fmt.Println()
		fmt.Println(sig)
		// Signal that the program is done.
		done <- true
	}()

	fmt.Println("awaiting signal")

	// Wait for the program to be done (signal received).
	<-done
	fmt.Println("exiting")
}

Explanation:

  1. sigs := make(chan os.Signal, 1): Creates a buffered channel (sigs) to receive signals. The buffer size is set to 1 to avoid missing signals if multiple signals arrive quickly.

  2. signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM): Notifies the program to listen for SIGINT (Ctrl+C) and SIGTERM (termination request) signals. When a signal is received, it will be sent to the sigs channel.

  3. done := make(chan bool, 1): Creates a buffered channel (done) to signal when the program is done.

  4. go func() { ... }(): Starts a goroutine to handle signals. Inside the goroutine, it waits for a signal on the sigs channel, prints the received signal, and signals that the program is done by sending a value to the done channel.

  5. fmt.Println("awaiting signal"): Prints a message indicating that the program is waiting for a signal.

  6. <-done: Waits for the program to be done (signal received) by receiving a value from the done channel.

  7. fmt.Println("exiting"): Prints a message indicating that the program is exiting.

This code allows the program to gracefully handle termination signals and perform necessary cleanup before exiting.

Last updated