Go Context Using a context to cancel work


Passing a context with a timeout (or with a cancel function) to a long running function can be used to cancel that functions work:

ctx, _ := context.WithTimeout(context.Background(), 200*time.Millisecond)
for {
    select {
    case <-ctx.Done():
        return ctx.Err()
        // Do an iteration of some long running work here!