Context is a tree of values
suggest changeContext is immutable.
To create a new context, you wrap existing context and add more data.
You can wrap context multiple time so you can think of it 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).
Cancelation signals travel down the tree. If a context is canceled, all of its children are also canceled.
The cancelation signal flow is represented by the grey edges.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents