Change db.close to defer db.close

This commit is contained in:
Eliot Whalan 2016-07-04 10:03:22 +10:00
parent cf42af990a
commit 9d97f561c2
1 changed files with 5 additions and 4 deletions

View File

@ -59,6 +59,7 @@ func generateName() string {
id := uniuri.NewLen(LENGTH) id := uniuri.NewLen(LENGTH)
db, err := sql.Open("mysql", DATABASE) db, err := sql.Open("mysql", DATABASE)
check(err) check(err)
defer db.Close()
query, err := db.Query("select id from pastebin where id=?", id) query, err := db.Query("select id from pastebin where id=?", id)
if err != sql.ErrNoRows { if err != sql.ErrNoRows {
@ -66,7 +67,6 @@ func generateName() string {
generateName() generateName()
} }
} }
db.Close()
return id return id
@ -102,6 +102,7 @@ func save(raw string, lang string, title string, expiry string) Response {
db, err := sql.Open("mysql", DATABASE) db, err := sql.Open("mysql", DATABASE)
check(err) check(err)
defer db.Close()
sha := hash(raw) sha := hash(raw)
query, err := db.Query("select id, title, hash, data, delkey from pastebin where hash=?", sha) query, err := db.Query("select id, title, hash, data, delkey from pastebin where hash=?", sha)
@ -136,7 +137,6 @@ func save(raw string, lang string, title string, expiry string) Response {
_, err = stmt.Exec(id, html.EscapeString(title), sha, dataEscaped, delKey, expiryTime) _, err = stmt.Exec(id, html.EscapeString(title), sha, dataEscaped, delKey, expiryTime)
check(err) check(err)
} }
db.Close()
return Response{id, title, sha, url, len(dataEscaped), delKey} return Response{id, title, sha, url, len(dataEscaped), delKey}
} }
@ -147,6 +147,7 @@ func delHandler(w http.ResponseWriter, r *http.Request) {
db, err := sql.Open("mysql", DATABASE) db, err := sql.Open("mysql", DATABASE)
check(err) check(err)
defer db.Close()
stmt, err := db.Prepare("delete from pastebin where delkey=? and id=?") stmt, err := db.Prepare("delete from pastebin where delkey=? and id=?")
check(err) check(err)
@ -160,7 +161,6 @@ func delHandler(w http.ResponseWriter, r *http.Request) {
} else { } else {
io.WriteString(w, id+" deleted") io.WriteString(w, id+" deleted")
} }
db.Close()
} }
func saveHandler(w http.ResponseWriter, r *http.Request) { func saveHandler(w http.ResponseWriter, r *http.Request) {
@ -228,6 +228,8 @@ func highlight(s string, lang string) (string, error) {
func getPaste(paste string, lang string) (string, string) { func getPaste(paste string, lang string) (string, string) {
param1 := html.EscapeString(paste) param1 := html.EscapeString(paste)
db, err := sql.Open("mysql", DATABASE) db, err := sql.Open("mysql", DATABASE)
check(err)
defer db.Close()
var title, s string var title, s string
var expiry string var expiry string
err = db.QueryRow("select title, data, expiry from pastebin where id=?", param1).Scan(&title, &s, &expiry) err = db.QueryRow("select title, data, expiry from pastebin where id=?", param1).Scan(&title, &s, &expiry)
@ -239,7 +241,6 @@ func getPaste(paste string, lang string) (string, string) {
check(err) check(err)
return "Error invalid paste", "" return "Error invalid paste", ""
} }
db.Close()
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return "Error invalid paste", "" return "Error invalid paste", ""