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: 9e648ae424
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>
This commit is contained in:
Krushnal Patil
2026-03-09 01:31:02 +00:00
committed by Gopher Robot
parent a95f60871f
commit e22dc6c866

View File

@@ -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()