Typed Context Keys with Generics

Store request-scoped values in context.Context with generic key types so lookups are type-safe and cannot collide by string.

GoResilience

Code

gosnippet
31 lines
package main

import (
	"context"
	"fmt"
)

type key[T any] struct{ name string }

func WithValue[T any](ctx context.Context, k key[T], value T) context.Context {
	return context.WithValue(ctx, k, value)
}

func Value[T any](ctx context.Context, k key[T]) (T, bool) {
	value, ok := ctx.Value(k).(T)
	return value, ok
}

type RequestID string

var requestIDKey = key[RequestID]{name: "request-id"}

func main() {
	ctx := WithValue(context.Background(), requestIDKey, RequestID("req-42"))
	id, ok := Value(ctx, requestIDKey)
	fmt.Println(id, ok)

	_, missing := Value(ctx, key[int]{name: "attempt"})
	fmt.Println("missing:", missing)
}

How it works

Typed Context Keys with Generics reduces to the function main.

Applying it

The file’s steps show up in the output as missing:.

Output is labeled for readability — expect lines prefixed with missing:.

Statements that matter

func WithValue[T any](ctx context.Context, k key[T], value T) context.Context {
	return context.WithValue(ctx, k, value)
}
func Value[T any](ctx context.Context, k key[T]) (T, bool) {
	value, ok := ctx.Value(k).(T)
	return value, ok
}
func main() {
	ctx := WithValue(context.Background(), requestIDKey, RequestID("req-42"))
	id, ok := Value(ctx, requestIDKey)
	fmt.Println(id, ok)
	_, missing := Value(ctx, key[int]{name: "attempt"})
	fmt.Println("missing:", missing)
}

Interactive Tools for This Topic

FAQ

What does Typed Context Keys with Generics print when run?
The script runs standalone; its 1 labeled outputs include `missing:`.