Kokizzu
Kokizzu

Reputation: 26838

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

From this tutorial shown that rows.Closed() must be called where rows is from stmt.Query(), is stmt.Closed() also should be called where stmt is 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)

Upvotes: 4

Views: 7033

Answers (2)

Nisal Edu
Nisal Edu

Reputation: 7591

Use as follows

// 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())
}

Upvotes: 0

joshua
joshua

Reputation: 4198

The short answer is Yes. You should call stmt.Close();

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

Upvotes: 6

Related Questions