Context package variables

April 23, 2025

The context package exports two variables:

Variables

var Canceled = errors.New("context canceled")

Canceled is the error returned by [Context.Err] when the context is canceled for some reason other than its deadline passing.

var DeadlineExceeded error = deadlineExceededError{}

DeadlineExceeded is the error returned by [Context.Err] when the context is canceled due to its deadline passing.

That’s it.

These are the only two types of errors that a context’s Err() method is allowed to return.

It’s frequently useful to check against one or both of these error types when examining an error. As an example, when a database method returns context.Canceled in the context of serving a web request, you may not want to log the error–it’s an indication that the client canceled the request before it was served, but you would want to log a context.DeadlineExceeded error, because that means your database is taking too long.

err := db.Exec(ctx, /* ... some interesting query ... */)
switch {
case errors.Is(err, context.Canceled):
	// Don't report the error to the caller, just return early
	return nil
case err != nil:
	// All other errors should be returned to the caller, for handling or logging,
	// includding deadline exceeded errors.
	return err
}

/* continue with other processing */

Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


The Context API contract

Today we come to the core of the context package: The Context interface itself. type Context type Context interface { Deadline() (deadline time.Time, ok bool) Done() <-chan struct{} Err() error Value(key any) any } A Context carries a deadline, a cancellation signal, and other values across API boundaries. Context’s methods may be called by multiple goroutines simultaneously. For clarity, I’ve removed all of the documentation for each of the interface methods in the above quote—we’ll get to those in following emails, and include those there.


Contexts with timeouts

func WithTimeout func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)). Canceling this context releases resources associated with it, so code should call cancel as soon as the operations running in this Context complete: func slowOperationWithTimeout(ctx context.Context) (Result, error) { ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond) defer cancel() // releases resources if slowOperation completes before timeout elapses return slowOperation(ctx) } Not only is WithTimeout conceptually similar to WithDeadline, it literally is WithDeadline, as we can see form the source code:


Contexts with deadlines

Today we’re talking about canceling contexts based on time. Arguably, this should have come first, as it’s a little simpler to work with than the explicit cancelation we talked about a week ago. But once again, I’m going in essentially alphabetical order, so it’s what we have… func WithDeadline func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) WithDeadline returns a derived context that points to the parent context but has the deadline adjusted to be no later than d.

Get daily content like this in your inbox!

Subscribe