params.go 507 B

123456789101112131415161718192021222324252627
  1. package context
  2. import (
  3. "context"
  4. "net/http"
  5. )
  6. var pathVars = contextKey("pathVars")
  7. func Vars(r *http.Request) map[string]string {
  8. vars, ok := r.Context().Value(pathVars).(map[string]string)
  9. if ok {
  10. return vars
  11. }
  12. return nil
  13. }
  14. func WithPathVars(r *http.Request, params map[string]string) *http.Request {
  15. return r.WithContext(context.WithValue(r.Context(), pathVars, params))
  16. }
  17. type contextKey string
  18. func (c contextKey) String() string {
  19. return "rest/internal/context key: " + string(c)
  20. }