Attach a simple map[string]any to a context.Context.
go get github.com/Lexographics/mapcontextctx := mapcontext.PrepareContext(context.Background())
mapcontext.AddContextValue(ctx, "traceID", "abc-123")
mapcontext.AddContextValue(ctx, "userID", 42)
traceID, ok := mapcontext.GetContextValueAs[string](ctx, "traceID")
userID, ok := mapcontext.GetContextValueAs[int](ctx, "userID")PrepareContext(ctx): ensure the context has an internal map (idempotent).IsPrepared(ctx): check if the map is present.AddContextValue(ctx, key, value): set or overwrite a value.GetContextValue(ctx, key) (any, bool): get a value.GetContextValueAs[T](ctx, key) (T, bool): get and type-assert.GetContextValues(ctx) Map: access the internal map.
GetContextValuesreturns the shared map; edits affect the stored values.- If the context isn’t prepared,
AddContextValuedoes nothing.PrepareContextmust be called before. - No locking is provided; add your own if you share a context across goroutines.
See examples/main/main.go for a tiny runnable example.
Run tests:
go test ./...