Skip to content

Instantly share code, notes, and snippets.

@ae6rt
Created November 30, 2014 01:59
Show Gist options
  • Select an option

  • Save ae6rt/a5bbe626f5334f976bd8 to your computer and use it in GitHub Desktop.

Select an option

Save ae6rt/a5bbe626f5334f976bd8 to your computer and use it in GitHub Desktop.
A shutdown-hook scaffold for Go
package lifecycle
import (
"log"
"os"
"os/signal"
)
var targets []Shutdowner
type Shutdowner interface {
Shutdown()
}
func init() {
targets = make([]Shutdowner, 0)
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
<-c
log.Println("Shutdown...")
for _, sd := range targets {
sd.Shutdown()
}
os.Exit(0)
}()
}
func AddShutdownHook(sd Shutdowner) {
targets = append(targets, sd)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment