Go context.DeadlineExceeded
A context reports DeadlineExceeded when its time budget expires.
A simple example
context.DeadlineExceeded
- Start with An unfinished task
- Do this Wait only until a deadline
- Result Time limit reached
Syntax
errors.Is(ctx.Err(), context.DeadlineExceeded)
Run these examples in Go
Save one example at a time as main.go. Use Go 1.21 or later.
go run main.goLimit the wait for a shipping quote
A checkout screen has used its entire waiting budget for a carrier quote. The quote is still pending, so the customer should see a pending message while the underlying work remains available.
package main
import (
"context"
"errors"
"fmt"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 0)
defer cancel()
<-ctx.Done()
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
fmt.Println("Quote pending")
}
}
Quote pending
There is no result within the zero-length wait, so checkout reports a pending quote immediately.
Poll a finished download
A document download has already completed when the kiosk checks. Inspect the job using a zero-wait poll. A job that has yet to complete stays in the waiting queue.
Poll a pending proof
A designer checks a pending proof without blocking the screen. Inspect the job using a zero-wait poll. A job that has yet to complete stays in the waiting queue.
What to watch for
A worker must observe the context for cancellation to stop its own work.