type Context interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key any) any
}
- Go's focus on concurrency resulting in need of interrupting spawned goroutines
- Done's return value tells if the parent function in the chain has asked to stop the work
- Go was also supposed to work as a language for microservices or systems interacting with external resources.
- Whenever we are interacting with external systems, it is better to set a deadline for that interaction
- Err returns one of two types of values. Either
context.Canceled
error orcontext.DeadlineExceeded
. - It just differenciates why the context says that work needs to be stopped/cancelled
- Probably the only field that makes sense of the package name
- Extensively used in server side code to store request scoped variables like "authenticated_user","trace_id", "role", etc.