A context can carry request-scoped values through function calls.

Context Values

context_values.go
package main

import (
	"context"
	"fmt"
)

type contextKey string

func main() {
	var user = 
	key := contextKey("user")
	ctx := context.WithValue(context.Background(), key, user)

	value := ctx.Value(key)
	fmt.Println("user=", value)
	fmt.Println("known=", value != nil)
}
package main

import (
	"context"
	"fmt"
)

type contextKey string

func main() {
	var user = 
	key := contextKey("user")
	ctx := context.WithValue(context.Background(), key, user)

	value := ctx.Value(key)
	fmt.Println("user=", value)
	fmt.Println("known=", value != nil)
}
package main

import (
	"context"
	"fmt"
)

type contextKey string

func main() {
	var user = 
	key := contextKey("user")
	ctx := context.WithValue(context.Background(), key, user)

	value := ctx.Value(key)
	fmt.Println("user=", value)
	fmt.Println("known=", value != nil)
}
context value Context values are best for small request data that needs to travel with the context.