Table of Contents

WithDeadline

What is it?

`context.WithDeadline(parent, t)` creates a child context that is canceled at the specific time `t` (a `time.Time`).

Signature: `ctx, cancel := context.WithDeadline(parent, t)`

What is it used for?

Example

deadline := time.Now().Add(1500 * time.Millisecond)
ctx, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
 
select {
case <-ctx.Done():
    // deadline exceeded or canceled
default:
    // do work
}

Notes / pitfalls

Hard words (English)