context

package
v2.303.5 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2026 License: MIT Imports: 2 Imported by: 13

Documentation

Overview

Package context provides small wrappers and aliases around the standard library context package.

The primary purpose of this package is to offer a stable go-service import path for commonly used context primitives while preserving the exact semantics of the standard library `context` package.

Most identifiers are thin aliases/wrappers around `context` (for example `Context`, `CancelFunc`, `Background`, `WithDeadline`, `WithTimeout`, and `WithValue`). They exist so packages within go-service and downstream services can consistently import `github.com/alexfalkowski/go-service/v2/context` without mixing direct stdlib imports across the codebase.

This package also defines `Key`, a typed helper for context value keys to reduce accidental collisions when multiple packages store values in the same context.

Index

Constants

This section is empty.

Variables

View Source
var Canceled = context.Canceled

Canceled is an alias for context.Canceled.

It is returned by Context.Err when the context is canceled.

Functions

func WithDeadline

func WithDeadline(parent Context, d time.Time) (Context, CancelFunc)

WithDeadline returns a copy of parent with the deadline adjusted to be no later than d.

This is a thin wrapper around context.WithDeadline. The returned CancelFunc should be called to release resources associated with the derived context.

func WithTimeout

func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)

WithTimeout returns a copy of parent with a timeout applied.

This is a thin wrapper around context.WithTimeout. The returned CancelFunc should be called to release resources associated with the derived context.

Types

type CancelFunc

type CancelFunc = context.CancelFunc

CancelFunc is an alias for context.CancelFunc.

It cancels a Context created by WithDeadline or WithTimeout, releasing resources associated with it. As with the standard library, you should call the returned CancelFunc as soon as the operations running in the derived context complete.

type Context

type Context = context.Context

Context is an alias for context.Context.

It carries deadlines, cancellation signals, and request-scoped values across API boundaries. The semantics are identical to the standard library context package.

func Background

func Background() Context

Background returns an empty, non-cancelable context.

This is a thin wrapper around context.Background. Use it as the top-level parent for main, initialization, and tests. Request handlers should typically use the request-provided context instead.

func WithValue

func WithValue(parent Context, key, val any) Context

WithValue returns a copy of parent in which the value associated with key is val.

This is a thin wrapper around context.WithValue.

Best practice: use context values only for request-scoped data that transits process boundaries (like trace IDs). Do not use them to pass optional parameters to functions.

type Key

type Key string

Key is a typed helper for storing values in a context.

Using a distinct key type reduces accidental collisions when multiple packages store values in the same context. Prefer defining keys as unexported package variables, e.g.:

var userIDKey context.Key = "user_id"

and retrieving values with type assertions at the call site.

Jump to

Keyboard shortcuts

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