When using http.ListenAndServe()
in Go, this results in a blocking situation where the application, apparently, can only be stopped by killing it. This seems to skip processing my defer
statements. Please see the code below. When I kill the application the db is not closed. How can I make sure my defer
statement will be run?
func main() {
db := NewDB(DBFILENAME)
defer db.Close()
http.HandleFunc("/", handler)
http.ListenAndServe(":80", nil)
}