ctxutil

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetContextValue

func GetContextValue[T any](ctx context.Context, key any, returnOnNull T) T

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

func NewContext(fromCtx context.Context) context.Context

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

func SetContextValue(
	ctx context.Context, key any, data any,
) (context.Context, error)

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.

Jump to

Keyboard shortcuts

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