Canonical guidance

Use when

Avoid

Preferred pattern

ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()

row := db.QueryRowContext(ctx, q, id)

Anti-pattern

Explanation: This anti-pattern is tempting because context-free calls are shorter, but they let slow queries outlive the request or job that needed them.

Why

Sources