Skip to content
This repository was archived by the owner on May 24, 2023. It is now read-only.

Update accessing.md#120

Closed
Ely0rda wants to merge 1 commit intoVividCortex:gh-pagesfrom
Ely0rda:patch-1
Closed

Update accessing.md#120
Ely0rda wants to merge 1 commit intoVividCortex:gh-pagesfrom
Ely0rda:patch-1

Conversation

@Ely0rda
Copy link
Copy Markdown

@Ely0rda Ely0rda commented Jan 24, 2023

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

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**
Comment thread accessing.md
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.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ely0rda

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

@Ely0rda Ely0rda closed this Jan 24, 2023
@Ely0rda Ely0rda deleted the patch-1 branch January 24, 2023 16:37
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants