Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync: Proposal: Add a Go() function to sync.WaitGroup #23538

Closed
pkern opened this issue Jan 24, 2018 · 1 comment
Closed

sync: Proposal: Add a Go() function to sync.WaitGroup #23538

pkern opened this issue Jan 24, 2018 · 1 comment

Comments

@pkern
Copy link

pkern commented Jan 24, 2018

sync.WaitGroup offers a flexible way to wait for an arbitrary number of tasks submitted. In golang.org/x/sync/errgroup we see a neat method called Go(func() error) that spawns a goroutine and makes sure that the internal counters are adjusted appropriately. Would this be a useful addition to sync.WaitGroup as well? Essentially it'd be used like this:

var wg sync.WaitGroup
wg.Go(func() { ... })
wg.Wait()

Internally the implementation would be very simple:

// Go calls the given function in a goroutine and adds it to the WaitGroup.
func (wg *WaitGroup) Go(f func()) {
	wg.Add(1)
	go func() {
		defer wg.Done()
		f()
	}()
}

A fair argument against this would be "just use errgroup", which feels incredibly useful whenever you fan out to multiple goroutines and wait for their result, especially as it deals with cancellation properly. Is there maybe a possibility to put that library into core Go instead?

This was mostly spawned by a code review where a reviewer take issue with the use of an errgroup with no error involved, as all errors were supposed to be ignored, and hence suggesting the use of sync.WaitGroup instead.

@gopherbot gopherbot added this to the Proposal milestone Jan 24, 2018
@dsnet
Copy link
Member

dsnet commented Jan 24, 2018

Closing as duplicate of #18022, which proposed the same thing.

@dsnet dsnet closed this as completed Jan 24, 2018
@mikioh mikioh changed the title Proposal: Add a Go() function to sync.WaitGroup sync: Proposal: Add a Go() function to sync.WaitGroup Jan 26, 2018
@golang golang locked and limited conversation to collaborators Jan 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants