context.go 427 B

1234567891011121314151617181920212223
  1. package util
  2. import (
  3. "context"
  4. "net/http"
  5. )
  6. // ContextKey context key type
  7. type ContextKey string
  8. // ContextGet get from context
  9. func ContextGet(ctx context.Context, key interface{}) interface{} {
  10. return ctx.Value(key)
  11. }
  12. // ContextSet set to context
  13. func ContextSet(r *http.Request, key, val interface{}) *http.Request {
  14. if val == nil {
  15. return r
  16. }
  17. return r.WithContext(context.WithValue(r.Context(), key, val))
  18. }