ctxutil

package module
v0.0.0-...-edffb83 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2025 License: MIT Imports: 2 Imported by: 0

README

ctxutil

Go Report Card

A tiny Go package that makes working with context.Context values less painful.

Why?

Working with context values in Go traditionally requires a lot of boilerplate and type assertions:

// 🤢 without ctxutil
type deviceIDKey struct{}
ctx = context.WithValue(ctx, deviceIDKey{}, "device-123")
deviceID, _ := ctx.Value(deviceIDKey{}).(string) // type assertion every time

// 😎 With ctxutil
ctx = ctxutil.SetDeviceID(ctx, "device-123")
deviceID := ctxutil.GetDeviceID(ctx) // strongly typed, no assertions

Additionally, extending a context timeout while preserving values is annoying:

// 🤢 without ctxutil values don't carry over
newCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// Now you need to manually copy all values...

// 😎 with ctxutil
newCtx, cancel := ctxutil.ExtendTimeout(ctx, 5*time.Second) // values are preserved
// works for both adding a timeout to a context without one
// and for replacing/extending an existing timeout

Install

go get github.com/alesr/ctxutil

Usage

import (
    "context"
    "github.com/alesr/ctxutil"
)

func main() {
    // store values in context
    ctx := context.Background()
    ctx = ctxutil.SetDeviceID(ctx, "device-123")
    ctx = ctxutil.SetTraceID(ctx, "trace-abc")

    // retrieve values
    deviceID := ctxutil.GetDeviceID(ctx) // "device-123"
    traceID := ctxutil.GetTraceID(ctx)   // "trace-abc"

    // add or extend context timeout while preserving values
    // Works whether ctx already has a timeout or not
    newCtx, cancel := ctxutil.ExtendTimeout(ctx, 5*time.Second)
    defer cancel()

    // values are still accessible
    deviceID = ctxutil.GetDeviceID(newCtx) // "device-123"
}

Development

Testing

This code uses the synctest.

GOEXPERIMENT=synctest go test -v -race -count=1 ./...

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtendTimeout

func ExtendTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc)

ExtendTimeout creates a fresh context with the given timeout and carries over known values from the original context. It works both for adding a timeout to contexts without one and for replacing/extending an existing timeout.

func GetDeviceID

func GetDeviceID(ctx context.Context) string

GetDeviceID gets the device ID from the context.

func GetTraceID

func GetTraceID(ctx context.Context) string

GetTraceID gets the trace ID from the context.

func SetDeviceID

func SetDeviceID(ctx context.Context, deviceID string) context.Context

SetDeviceID sets the device ID in the context.

func SetTraceID

func SetTraceID(ctx context.Context, traceID string) context.Context

SetTraceID sets the trace ID in the context.

Types

This section is empty.

Jump to

Keyboard shortcuts

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