context.go 492 B

12345678910111213141516171819202122232425
  1. package ctx
  2. import "context"
  3. type SessionKey int
  4. // ID of a session.
  5. type ID uint32
  6. const (
  7. idSessionKey SessionKey = 0
  8. )
  9. // ContextWithID returns a new context with the given ID.
  10. func ContextWithID(ctx context.Context, id ID) context.Context {
  11. return context.WithValue(ctx, idSessionKey, id)
  12. }
  13. // IDFromContext returns ID in this context, or 0 if not contained.
  14. func IDFromContext(ctx context.Context) ID {
  15. if id, ok := ctx.Value(idSessionKey).(ID); ok {
  16. return id
  17. }
  18. return 0
  19. }