context
The Go standard library package whose Context value carries a deadline, a cancellation signal and request-scoped values through a chain of function calls.
In more detail
By convention a Context is the first parameter, named ctx. context.Background() is the empty root that others are derived from. Functions watch ctx.Done() and stop with ctx.Err() when it closes. Keep ordinary configuration in parameters, not in context values.
Example
func report(ctx context.Context) {}Related terms
- context cancellation — Telling every function using a context to stop, by calling the cancel function returned when the context was created.
- context deadline — A time limit attached to a context, after which it is cancelled automatically and ctx.Err() reports context.DeadlineExceeded.
- context value — Data attached to a context with context.WithValue and read back with ctx.Value, meant for request-scoped details such as a trace label.
- goroutine — A function running concurrently with the rest of a Go program, started by putting the go keyword before a call.
Taught in
- Errors as Values — Concurrent Systems Programming with Go
- Wrapping Errors and errors.Is — Concurrent Systems Programming with Go
- Panics and Recover — Concurrent Systems Programming with Go
- The context Package — Concurrent Systems Programming with Go
- Context Cancellation and Timeouts — Concurrent Systems Programming with Go
- The time Package — Concurrent Systems Programming with Go
- Calling APIs with net/http Client — Concurrent Systems Programming with Go
- Graceful Shutdown on SIGTERM — Concurrent Systems Programming with Go