Skip to content

mraerino/typed-context

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

6 Commits

Repository files navigation

Typed Context Access for Go

Using generics to access values in a context.Context.

This makes sure that you never need to cast the values you get from ctx.Value at runtime.

Example

package main import ( "context""fmt" typedcontext "github.com/mraerino/typed-context" ) typeRequestIDstringfuncmain(){ctx:=context.Background() // set a valuectx=typedcontext.Set(ctx, RequestID("12345")) // get a valuerequestID, ok:= typedcontext.Get[RequestID](ctx) if!ok{panic("Not found!") } fmt.Printf("Request ID: %s\n", requestID) }

Run in the Playground

Performance

In a micro-benchmark, this library performs similarly to using context.WithValue manually.

goos: darwin goarch: amd64 pkg: github.com/mraerino/typed-context cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz BenchmarkStdlib-16 203086635 5.857 ns/op 0 B/op 0 allocs/op BenchmarkTyped-16 194297246 6.054 ns/op 0 B/op 0 allocs/op PASS ok github.com/mraerino/typed-context 3.712s 

About

Type-based context access in Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages