It may or may not panic. General rule is to always check the returned error first, and if that is nil
, only then proceed to use other return values.
If you get a non-nil
error, other return values are "undefined", they may be nil
, and thus calling their methods may easily result in a runtime panic.
There may be exceptions of course which are usually documented, e.g. http.Get()
may return an error and a non-nil
response if following redirection fails, providing details about the error. But this is rare, and should always be documented. DB.Query()
does not document such deviation, so you can't rely on that.
See related: Do we need to close the response object if an error occurs while calling http.Get(url)?
rows
can be nil or otherwise invalid. Just don't do that. That is reference enough.nil
, why would you want to close that? and how would you do it without causing panic?rows.Close
before error checking but also avoid panics? You can check ifrows
is notnil
and if it isn't callrows.Close
, after that block check the error value. Ultimately it is your code, you do whatever you want however you want...