Skip to content

Lexographics/mapcontext

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mapcontext

Attach a simple map[string]any to a context.Context.

Install

go get github.com/Lexographics/mapcontext

Quick start

ctx := 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")

Functions

  • 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.

Notes

  • GetContextValues returns the shared map; edits affect the stored values.
  • If the context isn’t prepared, AddContextValue does nothing. PrepareContext must 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 ./...

About

Attach a simple map[string]any to a context.Context

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages