When working with context values, it's good to create a new type to represent the key. In this case, we created a key type, then declared some corresponding const values to represent all of our possible keys.
In this case, we initialize all our key/value pairs at the same time using the Setup() function. When modifying contexts, functions generally take a context argument and return a context value. So, the signature often looks like this:
func ModifyContext(ctx context.Context) context.Context
Sometimes, these methods also return an error or theĀ cancel() function, such as in the cases of context.WithCancel, context.WithTimeout, and context.WithDeadline. All child contexts inherit the attributes of the parent.
In this recipe, we created two child contexts, one with a deadline and one with a timeout. We set these to timeout to be random ranges, then terminated when either is received. Lastly, we extracted a value given a set key and printed it.