Skip to content

Add note about QueuedQuery.Exec and BatchResults.Close#2372

Merged
jackc merged 1 commit into
jackc:masterfrom
WGH-:improve-batch-doc
Aug 30, 2025
Merged

Add note about QueuedQuery.Exec and BatchResults.Close#2372
jackc merged 1 commit into
jackc:masterfrom
WGH-:improve-batch-doc

Conversation

@WGH-

@WGH- WGH- commented Aug 20, 2025

Copy link
Copy Markdown
Contributor

When reviewing my own old code, I noticed several monstrosities like this:

batchResult := tx.SendBatch(ctx, batch)
defer batchResult.Close()
for range batch.Len() {
        if _, err := batchResult.Exec(); err != nil {
                return err
        }
}
return nil

All of them can replaced with just this:

return tx.SendBatch(ctx, batch).Close()

So I thought it might be a good idea to give a hint in the docs explicitly. This trick is not so apparent, after all.

When reviewing my own old code, I noticed several monstrosities like
this:

    batchResult := tx.SendBatch(ctx, batch)
    defer batchResult.Close()
    for range batch.Len() {
            if _, err := batchResult.Exec(); err != nil {
                    return err
            }
    }
    return nil

All of them can replaced with just this:

    return tx.SendBatch(ctx, batch).Close()

So I thought it might be a good idea to give a hint in the docs
explicitly. This trick is not so apparent, after all.
@WGH- WGH- force-pushed the improve-batch-doc branch from 614f4c9 to 248afe6 Compare August 20, 2025 06:41
@jackc jackc merged commit 82fbe49 into jackc:master Aug 30, 2025
14 checks passed
@jackc

jackc commented Aug 30, 2025

Copy link
Copy Markdown
Owner

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants