Skip to content

Instantly share code, notes, and snippets.

@RepComm
Created April 2, 2024 19:51
Show Gist options
  • Save RepComm/6fa2e6ee10799626245733b65ed537b8 to your computer and use it in GitHub Desktop.
Save RepComm/6fa2e6ee10799626245733b65ed537b8 to your computer and use it in GitHub Desktop.
handle signal termination and interupt in golang
package common
import (
"os"
"os/signal"
"syscall"
)
func HandleSigTerm(cb func(sig os.Signal)) {
signalChannel := make(chan os.Signal, 2)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
go func() {
sig := <-signalChannel
switch sig {
case os.Interrupt:
cb(sig)
case syscall.SIGTERM:
cb(sig)
}
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment