Files

30 lines
583 B
Go
Raw Permalink Normal View History

2025-12-10 18:33:37 +08:00
package utils
import (
"context"
"github.com/google/uuid"
)
type contextKey string
const RequestIDKey contextKey = "request_id"
const RequestIDHeaderKey = "X-Request-ID"
func NewContextWithRequestID(ctx context.Context, requestID string) context.Context {
newCtx := context.Background()
newCtx = context.WithValue(newCtx, RequestIDKey, requestID)
return newCtx
}
func NewUUID() string {
return uuid.New().String()
}
func GetRequestIDFromContext(ctx context.Context) string {
if requestID, ok := ctx.Value(RequestIDKey).(string); ok {
return requestID
}
return ""
}