Update accessing.md#120
Conversation
It seems that there is a typo here "It is idiomatic to defer db.Close() **if** the sql.DB should not have a lifetime beyond the scope of the function." So I tried making it more clear **because the `sql.DB` object**
| 2. The second argument is a driver-specific syntax that tells the driver how to access the underlying datastore. In this example, we're connecting to the "hello" database inside a local MySQL server instance. | ||
| 3. You should (almost) always check and handle errors returned from all `database/sql` operations. There are a few special cases that we'll discuss later where it doesn't make sense to do this. | ||
| 4. It is idiomatic to `defer db.Close()` if the `sql.DB` should not have a lifetime beyond the scope of the function. | ||
| 4. It is idiomatic to `defer db.Close()` , because the `sql.DB` object should not have a lifetime beyond the scope of the function. |
There was a problem hiding this comment.
See https://pkg.go.dev/database/sql#Open.
Based on what I've read, the *sql.DB value is usually long lived and will have a lifetime beyond the scope of a single function call. The proposed change explicitly indicates otherwise.
There was a problem hiding this comment.
Sorry, I just misunderstood the phrase, I thought you would want to close it after the main function ends, because its lifetime can go beyond the scope of main.
There was a problem hiding this comment.
@Ely0rda: Sorry, I just misunderstood the phrase
No problems, the statement is heavily dependent on the reader already knowing whether the value should have a lifetime beyond the scope of the function.
In the case of the main function in the example, I don't believe it should, but if you're passing a *sql.DB into a function then it will likely mean that you wish to have the lifetime be longer than that of a function call.
I still consider myself fairly new, so please double-check my feedback to make sure it's on point.
It seems that there is a typo here "It is idiomatic to defer db.Close() if the sql.DB should not have a lifetime beyond the scope of the function."
So I tried making it more clear because the
sql.DBobject