I am writing a simple Go web application using PostgreSQL. My main function looks like
var db *sql.DB func main() { var err error db, err = sql.Open("postgres", "...") if err != nil { log.Fatalf("Couldn't connect to the database: %v", err) } http.HandleFunc("/whatever", whateverHandler) http.ListenAndServe("127.0.0.1:8080", nil) }
It seems like I should call Close() in a database connection at some point, but when? This application works forever (i.e. until I kill it with ^C ). If I put the code after calling ListenAndServe , it will not run because my ^C killed the whole application. Should my application be structured differently?
source share