Canonical guidance

Use when

Avoid

Preferred pattern

rows, err := db.QueryContext(ctx, q, accountID)
if err != nil {
	return err
}
defer rows.Close()

for rows.Next() {
	// scan rows
}
return rows.Err()

Anti-pattern

Explanation: This anti-pattern is tempting because it centralizes setup, but it holds locks and resources longer than necessary.

Why

Related pages

Sources