Skip to content

Instantly share code, notes, and snippets.

@kendellfab
Created July 18, 2020 03:35
Show Gist options
  • Save kendellfab/d497e588af79083ae2231aa2d48d4cbe to your computer and use it in GitHub Desktop.
Save kendellfab/d497e588af79083ae2231aa2d48d4cbe to your computer and use it in GitHub Desktop.
import (
"context"
"net/http"
)
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