Created
April 2, 2024 19:51
-
-
Save RepComm/6fa2e6ee10799626245733b65ed537b8 to your computer and use it in GitHub Desktop.
handle signal termination and interupt in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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