Mahdi.Kh
September 21, 2026
We get familiar with context in Go and learn how to use it.
The context package is part of Go's standard library and provides the Context interface. Using this interface, you can define and manage a deadline, a timeout, and a cancellation signal for various operations.
Beyond that, context makes it possible to hold and pass along data whose scope and lifetime are limited to a specific request. This feature is especially useful in web applications and distributed services; for example, you can pass information such as an authenticated user's ID, a request ID, or transaction-related data between the different layers of an application through the context.
The Context interface has a simple structure, like this:
To create a Context, you can use the functions below:
context.Background: creates an initial, empty Context. It's usually used as a starting point, and you can then add a deadline and cancellation to it.context.TODO: if we need an empty context but don't yet know what we're going to do with the Context, we can use this function.context.WithCancel: this function can be used for cancellation, and generally for creating a cancellation signal.context.WithTimeout / context.WithDeadline: these functions can be used to create a timeout signal or a deadline signal, respectively.context.WithValue: using this function, you can put a value into the context and later retrieve it from the context using the Value function.contextUsing context, you can send a cancellation signal to goroutines so they stop their execution when needed. This feature is very useful for managing long-running operations, freeing up resources, and avoiding unnecessary processing.
In the example below, we first create an initial Context using context.Background(). Then, using context.WithCancel(), we get a cancelable Context and a cancel function. Using time.AfterFunc, the cancel function is called after one second, which cancels the Context, and the goroutines depending on it stop after receiving the signal from ctx.Done().
contextA deadline represents a specific point in the future by which an operation must complete. If the operation doesn't finish by then, it's automatically canceled.
In Go, you can use context.WithDeadline to set a specific end time for an operation.
contextA timeout is the amount of time allotted for an operation. If the operation doesn't complete within that time window, it's automatically stopped (canceled).
In Go, you can use context to set a time limit for an operation.
Context In Go, using context.WithValue, you can store a value inside a Context and then access it through the Value method.
Although context, through context.WithValue, makes it possible to store and pass values, according to Go's documentation, this feature should only be used for request-scoped data that moves between APIs and goroutines, and it shouldn't be used as a substitute for passing regular parameters or holding optional data in functions.
Contexts in Go are immutable. This means that every time we make a change, a new context is created that refers to the previous context and keeps it as its parent.
Canceling a context causes all of its descendant contexts to be canceled too, but ancestor contexts aren't affected and aren't canceled.
When we call the Value method, the value we want is first looked up in the current context. If it isn't found, the search continues in the parent context, and this process continues until it reaches the root context.
Contextcontext.Context as the first parameter to functions that need it.context.Value to pass data tied to a request's lifetime (such as requestID, traceID, or a logger), and avoid putting core business data in it.Err() method, you can tell whether a context was canceled because cancel() was called (context.Canceled) or because its time limit was exceeded (context.DeadlineExceeded).Previous part: Channels | Golang basics
Next part: Unit Testing | Golang basics