Signals
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")
}Last updated