From e22dc6c86685aef76a9bb22ff186d4cd5a822027 Mon Sep 17 00:00:00 2001 From: Krushnal Patil Date: Mon, 9 Mar 2026 01:31:02 +0000 Subject: [PATCH] context: modernize AfterFunc example using WaitGroup.Go The context.AfterFunc example currently uses the traditional sync.WaitGroup pattern with Add and Done. Update the example to use sync.WaitGroup.Go instead. Fixes #78018 Change-Id: I079a773a6ec1c65f26af2bd8092067843adb1cd1 GitHub-Last-Rev: 9e648ae4241ca0647d3a501f7658d48100c180b8 GitHub-Pull-Request: golang/go#78020 Reviewed-on: https://go-review.googlesource.com/c/go/+/752880 Reviewed-by: Damien Neil Reviewed-by: Mark Freeman LUCI-TryBot-Result: Go LUCI Auto-Submit: Damien Neil Reviewed-by: Ian Lance Taylor --- src/context/example_test.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/context/example_test.go b/src/context/example_test.go index be8cd8376e..1bed045e39 100644 --- a/src/context/example_test.go +++ b/src/context/example_test.go @@ -161,11 +161,8 @@ func ExampleAfterFunc_cond() { cond := sync.NewCond(new(sync.Mutex)) var wg sync.WaitGroup - for i := 0; i < 4; i++ { - wg.Add(1) - go func() { - defer wg.Done() - + for range 4 { + wg.Go(func() { ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond) defer cancel() @@ -174,7 +171,7 @@ func ExampleAfterFunc_cond() { err := waitOnCond(ctx, cond, func() bool { return false }) fmt.Println(err) - }() + }) } wg.Wait()