I'm working with postgresql using Golang, and I'm in a situation where I have to do several queries. If I store the results of each query in the same variable let's call it "rows", reassigning that variable each time to a new result of query, will it automatically close previous rows? Or should I each time close them manually? And if I should close them manually what happens if I defer rows.Close() each time? Is that so necessary?
How about calling Rows.Close()
*beforeyou assign to
rows` again?
And no, Rows.Close()
is not automatically called by a reassignment. If you do several different queries, use different variables to hold the rows returned by each. Call defer rows.Close()
for each after assigning.
You need to call rows.Close()
before each reassignment. By this way you will free memory from contents of "rows" pointer. Also, be careful with defer rows.Close()
, because:
rows
is not a pointer - it will call Close()
only for last assigned object.