Skip to content

Conversation

@WGH-
Copy link
Contributor

@WGH- WGH- commented Aug 20, 2025

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
Copy link
Owner

jackc commented Aug 30, 2025

LGTM

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

Labels

None yet

2 participants