Context package carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes.

Do not store Contexts inside a struct type; instead, pass a Context explicitly to each function that needs it. The Context should be the first parameter, typically named ctx:

Idea

When a request is canceled or times out, all the goroutines working on that request should exit quickly so the system can reclaim any resources they are using. It can be used to pass request-scoped values, cancellation signals, and deadlines across API boundaries to all the goroutines involved in handling a request.

deadline is an absolute time and a timeout is a relative time.

API

WithCancel

func WithCancel(parent Context) (ctx Context, cancel CancelFunc)