Skip to content

Instantly share code, notes, and snippets.

@bradkarels
Created December 21, 2023 16:04
Show Gist options
  • Save bradkarels/078b2fa4a184c39a1b0126c29a7481f3 to your computer and use it in GitHub Desktop.
Save bradkarels/078b2fa4a184c39a1b0126c29a7481f3 to your computer and use it in GitHub Desktop.
Simple example of using channels to listen for and act on SIGINT/SIGTERM signals.
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
)
// This program will run until interupted (e.g. ctrl+c).
// A channel is created to give Notify a place to send our designated signals
// A second channel waits for a signal to be "done"
// On SIGINT/SIGTERM we send true to done and exit
func main() {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
done := make(chan bool, 1)
go func() {
recSig := <-signals
fmt.Println()
fmt.Println(recSig)
done <- true
}()
fmt.Println("Don't worry I'll wait...")
<-done
fmt.Println("Ok, done waiting...")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment