context.Context
is a standard Go interface used to:
- Control cancellation of operations (e.g., HTTP requests, DB queries, goroutines)
- Set timeouts and deadlines
- Pass request-scoped values (like request ID or user ID) safely
It helps manage long-running processes, especially in concurrent or distributed systems.
type Context interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key any) any
}
- Prevents resource leaks
- Helps build cancellable, timeout-aware services
- Standardizes request metadata propagation
- Critical in HTTP servers, databases, microservices, etc.
Function | Description |
---|---|
context.Background() |
Root context (default starting point) |
context.TODO() |
Placeholder for future context logic |
context.WithCancel(ctx) |
Adds cancel capability |
context.WithTimeout(ctx, d) |
Cancels after timeout duration |
context.WithDeadline(ctx, t) |
Cancels at specific time |
context.WithValue(ctx, key, val) |
Adds value to context |
- Timeout for HTTP/DB calls
- Cancel goroutines cleanly
- Propagate request metadata (e.g., requestID)
- Graceful shutdowns (HTTP servers, background jobs)
Bad Practice | Why it's bad |
---|---|
Creating context.Background() in services |
Breaks cancellation chain |
Passing nil context |
Can panic, unclear behavior |
Storing context in struct fields | Misuse — context should flow by function |
- Always pass context as the first argument:
func(ctx context.Context, ...)
- Use
defer cancel()
afterWithCancel
,WithTimeout
, orWithDeadline
- Don’t abuse WithValue — use for control data, not app logic