Skip to content

Instantly share code, notes, and snippets.

@jnovack
Last active April 4, 2024 15:06
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jnovack/297cee036f3e5a430aa9444c0ae1b06d to your computer and use it in GitHub Desktop.
Save jnovack/297cee036f3e5a430aa9444c0ae1b06d to your computer and use it in GitHub Desktop.
Handle CTRL-C in Golang
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