如果我们将数据库/ sql行对象重新分配给新Query()的结果,会发生什么? 它会自动关闭吗? 还是我们应该自己关闭它?

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 torows` 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:

  1. You'll free rows only at function end, not immediately. Just keep this in mind.
  2. If rows is not a pointer - it will call Close() only for last assigned object.