context-pass-down-up

command module
v0.0.0-...-a4b7a9a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

README

Context Value - passing back from initialization function

Demonstrates how to encode values onto a context.Context through a sequential chain of function calls. This method may be useful during a high level initialization of components such as start-up.

Problem: in order to attach a value to a Context the WithValue() method is used and this method acomplishes the attachment by returning a new Context. The underlying implementation of the context does not support adding values, but rather creates a new Context with the value added.

Solution: follow the same pattern as context.WithValue and have each function in the chain return the (possibly updated) context as a return value.

Initial Question

Q: Is the context.Context passed into a function by reference?

Reason: If the context is passed in by reference then it is possible to attach a value to the context during an initialization function and have that value persist upon return, and into subsiquent initialization functions.

Is this a good practice? Unclear, this example is merely attempting to validate the premise of the question.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL