Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

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.

PreviousExec'ing ProcessesNextExit

Last updated 1 year ago

Was this helpful?