Handling Interrupts in Go

This little Go program does not run the defer statement when you interrupt by pressing ctrl-C: package main import "fmt" func main() { defer fmt.Println("Shutting down...") for { } } > go run interrupt.go ^Csignal: interrupt > You have to actually trap the signal and handle it. Fortunately that’s super simple in Go: package main import "fmt" import "os" import "os/signal" var shutdown = make(chan struct{}) func main() { defer fmt....