Tech article

What canceled my Go context?

Community description: 29 points · 16 comments on Hacker News

Hacker News | Mar 4, 2026 | mweibel

Automated excerpt

The caller of processOrder has no idea what actually happened. You’d think wrapping the error helps:if err := checkInventory(ctx, orderID); err ! = nil { return fmt. Errorf("checking inventory for %s: %w", orderID, err) context was canceled. Here’s the same processOrder rewritten to use it:func processOrder(ctx context. Context, orderID string) error { if err := checkInventory(ctx, orderID); err ! = nil { if err := chargePayment(ctx, orderID); err ! = nil { via %wNow you can read the cause with context. Cause(ctx). Err() will always return nil, Canceled, or DeadlineExceeded.

Selected automatically from source text; not independently written or fact-checked. Read the original for full context.

Read the original article

More tech news