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


Context errors

type Context type Context interface { … // If Done is not yet closed, Err returns nil. // If Done is closed, Err returns a non-nil error explaining why: // DeadlineExceeded if the context's deadline passed, // or Canceled if the context was canceled for some other reason. // After Err returns a non-nil error, successive calls to Err return the same error. Err() error Done() (covered yesterday) and Err() (today) are the two methods you’ll virtually alway use when writing code to honor context cancelations.


Why is context.TODO not just a comment?

I’ve been on vacation for the last week, so haven’t written much. Let’s finally finish up the context package series with some reader feedback and questions! Joost Helberg wrote in with an interesting observation about context.TODO. In his own words: With regards to context.TODO, back in my early Go days, I thought the TODO was about the intent of this context. Like context.Background is for a background task and context.WithCancel for a job that can be canceled.


context.WithValue

func WithValue func WithValue(parent Context, key, val any) Context WithValue returns a derived context that points to the parent Context. In the derived context, the value associated with key is val. Use context Values only for request-scoped data that transits processes and APIs, not for passing optional parameters to functions. The provided key must be comparable and should not be of type string or any other built-in type to avoid collisions between packages using context.

Get daily content like this in your inbox!

Subscribe