Context is a tree of values
Context is created by wrapping existing immutable context and adding additional information.
Since you can “branch” the same context multiple times, context value can be thought of as 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:
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.