Should we also close DB.Prepare () in Golang?

From this tutorial shows what rows.Closed()

should be called where rows

from stmt.Query()

, is stmt.Closed()

should also be where stmt

from db.Prepare()

?

// inside a function
stmt, err := db.Prepare(cmd) // cmd is SQL string
Check(err)
// should we add: defer stmt.Close()
rows, err := stmt.Query(params) // params is map/interface{}
defer rows.Close()
Check(err)

      

+3


source to share


2 answers


Short answer: Yes. You should call stmt.Close ();



The long answer can be found in this google groups thread.

+4


source


Use like this



// inside a function
stmt, err := db.Prepare(cmd) // cmd is SQL string
if err != nil {
    println(err.Error())
}
defer stmt.Close()
rows, err := stmt.Query(params) // params is map/interface{}

if err != nil {
    println(err.Error())
}

      

0


source







All Articles