Documentation
¶
Overview ¶
Package context provides utils for universal context.Context typed manipulations by generics.
Example ¶
package main import ( "context" "fmt" contextutil "source.toby3d.me/toby3d/hacks/context" ) func main() { type Username string var user Username = "toby3d" f := func(ctx context.Context) { // Retrieve typed value from parent context if exist. if name, exist := contextutil.Value[Username](ctx); exist { fmt.Printf("Hi, %s!\n", name) } else { fmt.Println("Access denied") } } ctx := context.Background() f(ctx) // Store some metadata in context. ctx = contextutil.WithValue(ctx, user) f(ctx) }
Output: Access denied Hi, toby3d!
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.