Last active
August 30, 2024 05:22
-
-
Save jnovack/297cee036f3e5a430aa9444c0ae1b06d to your computer and use it in GitHub Desktop.
Handle CTRL-C in Golang
This file contains 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 main | |
import ( | |
"fmt" | |
"os" | |
"os/signal" | |
"syscall" | |
"time" | |
) | |
func main() { | |
for { | |
fmt.Println("Logging") | |
time.Sleep(1500 * time.Millisecond) | |
} | |
} | |
func init() { | |
c := make(chan os.Signal) | |
signal.Notify(c, os.Interrupt, syscall.SIGTERM) | |
go func() { | |
<-c | |
// Run Cleanup | |
os.Exit(1) | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment