diff --git a/lectures/05-concurrency/context/cancelation/cancelation.go b/lectures/05-concurrency/context/cancelation/cancelation.go index 3a4939e..f2b7761 100644 --- a/lectures/05-concurrency/context/cancelation/cancelation.go +++ b/lectures/05-concurrency/context/cancelation/cancelation.go @@ -35,7 +35,7 @@ func doSlowJob(ctx context.Context) error { for { select { case <-ctx.Done(): - return ctx.Err() + return context.Cause(ctx) default: // perform a portion of slow job time.Sleep(1 * time.Second) diff --git a/lectures/05-concurrency/lecture.slide b/lectures/05-concurrency/lecture.slide index 202dd45..533fe50 100644 --- a/lectures/05-concurrency/lecture.slide +++ b/lectures/05-concurrency/lecture.slide @@ -372,6 +372,10 @@ Concurrency with Shared Memory ctx, cancel := context.WithCancel(ctx) defer cancel() + // manual cancel with explicit cause + ctx, cancel := context.WithCancelCause(ctx) + defer cancel(fmt.Errorf("job not needed")) + // cancel by timeout ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond) defer cancel()