Table of Contents

WithCancel

What is it?

`context.WithCancel(parent)` creates a child context that can be canceled manually by calling the returned `cancel()` function.

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

What is it used for?

Example

ctx, cancel := context.WithCancel(context.Background())
go func() {
    <-ctx.Done()
    // cleanup or stop work
}()
cancel()

Notes / pitfalls

Hard words (English)