ctxutil

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: May 16, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package ctxutil provides utilities for working with context.Context, including typed key-value storage and deadline helpers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Deadline

func Deadline(ctx context.Context, maxDuration time.Duration) time.Duration

Deadline returns the remaining time until the context deadline. If no deadline is set, returns maxDuration.

func IsExpired

func IsExpired(ctx context.Context) bool

IsExpired returns true if the context has been cancelled or its deadline has passed.

func Merge

func Merge(ctx1, ctx2 context.Context) (context.Context, context.CancelFunc)

Merge creates a context that is cancelled when either ctx1 or ctx2 is cancelled. Values are looked up from ctx1 first, then ctx2.

func MustValue

func MustValue[T any](ctx context.Context, key string) T

MustValue retrieves a typed value from context. Panics if the key is absent or the type doesn't match.

func Value

func Value[T any](ctx context.Context, key string) (T, bool)

Value retrieves a typed value from context. Returns the zero value of T and false if the key is absent or the type doesn't match.

func ValueOr

func ValueOr[T any](ctx context.Context, key string, defaultVal T) T

ValueOr retrieves a typed value from context, returning defaultVal if absent.

func WithValue

func WithValue(ctx context.Context, key string, val any) context.Context

WithValue stores a typed value in context under a namespaced key. The key string is converted to an unexported type to avoid collisions.

Types

This section is empty.

Jump to

Keyboard shortcuts

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