aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKrushnal Patil <krushnalpatil1111@gmail.com>2026-03-09 01:31:02 +0000
committerGopher Robot <gobot@golang.org>2026-03-10 20:06:37 -0700
commite22dc6c86685aef76a9bb22ff186d4cd5a822027 (patch)
tree0cb9e4077f7bb62ec9d95e6c64832ec763e83df0 /src
parenta95f60871f52f3701e596c07b0e7bf8a3b1619ed (diff)
downloadgo-e22dc6c86685aef76a9bb22ff186d4cd5a822027.tar.xz
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 <dneil@google.com> Reviewed-by: Mark Freeman <markfreeman@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Damien Neil <dneil@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/context/example_test.go9
1 files 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()