Context is a tree of values

suggest change

Context is immutable.

To create a new context, you wrap existing context and add more data.

You can wrap a context more than once, so you can think of derived contexts as a tree of values.

The following tree:

ctx := context.WithValue(
    context.WithDeadline(
        context.WithValue(context.Background(), sidKey, sid),
        time.Now().Add(30 * time.Minute),
    ),
    ridKey, rid,
)
trCtx := trace.NewContext(ctx, tr)
logCtx := myRequestLogging.NewContext(ctx, myRequestLogging.NewLogger())

can be visualized as:

Context represented as a directed graph.

Each child context has access to values of its parent contexts.

Data access flows upwards in the tree (represented by black edges).

Cancellation signals travel down the tree. If a context is canceled, all of its children are also canceled.

The cancellation signal flow is represented by the grey edges.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents