use package level defined contextKey as context key

This commit is contained in:
kevin
2020-09-20 12:46:35 +08:00
parent dc17855367
commit 86d3de4c89
3 changed files with 14 additions and 2 deletions

View File

@@ -5,7 +5,7 @@ import (
"net/http"
)
const pathVars = "pathVars"
var pathVars = contextKey("pathVars")
func Vars(r *http.Request) map[string]string {
vars, ok := r.Context().Value(pathVars).(map[string]string)
@@ -19,3 +19,9 @@ func Vars(r *http.Request) map[string]string {
func WithPathVars(r *http.Request, params map[string]string) *http.Request {
return r.WithContext(context.WithValue(r.Context(), pathVars, params))
}
type contextKey string
func (c contextKey) String() string {
return "rest/internal/context context key" + string(c)
}