Radar · Go · Archive · Week 15 · Apr 7 – 13, 2025
sync: add WaitGroup.Go
Worth knowingstdlib
- What changed
- A
Go(f func())method is added tosync.WaitGroup. It startsfin a new goroutine and tracks it with the WaitGroup. - Production impact
- The source does not say.
- Try it
- Create a
sync.WaitGroup, callwg.Go(func(){…}), and thenwg.Wait(). - Source
- github.com/golang/go/issues/63796
Understand it, then run it
sync.WaitGroup is a type you use to wait for several goroutines to finish. Before the change you had to write three lines for each goroutine: wg.Add(1), start the goroutine with go, and inside the goroutine call defer wg.Done(). The new Go method lets you write a single line that both starts the goroutine and registers it with the wait group. This removes a small but common source of bugs where the counter is mis‑counted or the goroutine is forgotten.
Run it now
// This program demonstrates the new sync.WaitGroup.Go method.
// It prints the numbers 1 through 5 in parallel, each on its own line.
package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
for i := 1; i <= 5; i++ {
// Capture loop variable to avoid the classic loopvar bug.
i := i
// Start a goroutine that prints the number and signals completion.
wg.Go(func() {
fmt.Println(i)
})
}
// Wait for all goroutines to finish.
wg.Wait()
}
What it printed when we ran it on Go 1.27.1
1 3 2 4 5
Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed