Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetContextValue ¶
GetContextValue tries to retrieve a value from the custom data of the context for a given key. If the key exists and the value matches the expected type, it returns the value. Otherwise, it returns the provided default value.
Parameters:
- ctx: The context from which to retrieve the value.
- key: The key for which to retrieve the value.
- returnOnNull: The default value to return if the key does not exist or the type does not match.
Returns:
- The value from the context if it exists and matches the expected type, otherwise the default value.
func NewContext ¶
NewContext initializes a new context with an empty contextData map.
Parameters:
- fromCtx: The context from which the new context is derived.
Returns:
- A new context with an initialized custom data map.
func SetContextValue ¶
SetContextValue sets a value in the custom data of the context for the provided key. It returns an error if the key is nil or if the custom context is not set.
Parameters:
- ctx: The context in which to set the value.
- key: The key for which to set the value.
- data: The value to set in the context.
Returns:
- The updated context.
- An error if the key is nil or if the custom context is not set.
Types ¶
type DataKey ¶
type DataKey int
DataKey is a unique key for storing custom data in the context.
func NewDataKey ¶
func NewDataKey() DataKey
NewDataKey safely increments and returns the next value of base. It is used to create a unique key for storing custom context data.
Returns:
- DataKey: The next data key value.