reqid.go 462 B

1234567891011121314151617181920
  1. package reqid
  2. import (
  3. "context"
  4. )
  5. type reqidKey struct{}
  6. // WithReqid 把reqid加入context中
  7. func WithReqid(ctx context.Context, reqid string) context.Context {
  8. return context.WithValue(ctx, reqidKey{}, reqid)
  9. }
  10. // ReqidFromContext 从context中获取reqid
  11. func ReqidFromContext(ctx context.Context) (reqid string, ok bool) {
  12. reqid, ok = ctx.Value(reqidKey{}).(string)
  13. return
  14. }
  15. // --------------------------------------------------------------------