🎯

Go Context Usage

Verified

by Community

Explains proper usage of Go's context package including cancellation propagation, timeout management, value passing, and common anti-patterns. Covers context in HTTP handlers, database calls, and goroutine management.

gocontextcancellationtimeoutrequest-scoped

Go Context Usage

Use Go's context package correctly for cancellation, timeouts, and request-scoped data.

Usage

Ask about context patterns or share code for guidance on proper context usage.

Examples

  • "How do I propagate cancellation through my call chain?"
  • "When should I use context.WithTimeout vs WithDeadline?"
  • "What values are appropriate to store in context?"

Guidelines

  • Always pass context as the first parameter named ctx
  • Use context for cancellation and deadlines, not for passing function arguments
  • Show proper context propagation through goroutines
  • Cover context in HTTP middleware and handlers
  • Warn about anti-patterns like storing context in structs