Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import (
type key int
var contextKey = key(22)
func SetUser(usr *User, r *http.Request) {
ctx := r.Context()
ctx = context.WithValue(ctx, contextKey, usr)
*r = *(r.WithContext(ctx))
func FromContext(r *http.Request) (*User, bool) {
usr, ok := r.Context().Value(contextKey).(*User)
return usr, ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment