-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: sync: add WaitGroup.Go method #39863
Comments
Could you explain more about the use cases here. From the example you gave, you’re running a function in a goroutine and waiting for it to finish — that’s the same as calling the function directly. |
CC @bcmills |
@davecheney of course that's not the whole usage :) I just didn't want to write more code than necessary to make it easier to read. I think it could apply any place you use WaitGroup where you're waiting other go routines to finish their computation. |
If you could give some examples that were closer to your intended usage that might help make your proposal more compelling. |
@davecheney wg := sync.WaitGroup{}
var urls = []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.somestupidname.com/",
}
for _, url := range urls {
// Launch a goroutine to fetch the URL.
url := url // https://golang.org/doc/faq#closures_and_goroutines
wg.Go(func() {
// Fetch the URL.
resp, err := http.Get(url)
if err == nil {
resp.Body.Close()
}
})
}
// Wait for all HTTP fetches to complete.
if err := wg.Wait(); err == nil {
fmt.Println("Successfully fetched all URLs.")
} |
I would rather add a semaphore to |
I agree that this would be a reasonable addition to Since |
@davecheney This is a very common pattern in our codebase, say I want to run 3 things in parallel. It'll be this: wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
doThis()
}()
go func() {
defer wg.Done()
doThat()
}()
doThose()
wg.Wait() vs this: wg := sync.WaitGroup{}
wg.Go(doThis)
wg.Go(doThat)
doThose()
wg.Wait() This is also less bug prone for the cases when people call wrong amount of Add or Done |
I've used a similar pattern that passes the WaitGroup to ops which may run independently:
|
I can't find the previous discussion, although I'm sure there is one. As for the chained ("fluent") calls, we've avoided that pattern in the Go standard library so far. It's just not idiomatic Go at this point. If you want to remember the receiver, use a variable. If the problem is forgetting the wg.Add to match wg.Done, it still seems like a vet check would be best (#18022). I'm not sure we ever did that. |
Now I see that the previous discussion is #18022. |
Based on the discussion above, I suggest we decline this issue and continue discussion of the vet check on the previously-filed #18022. Does anyone object to that? |
Based on the discussion above, this seems like a likely decline. |
No change in consensus, so declined. |
When I use errgroup.Group, I noticed I need much less code compared to sync.WaitGroup in certain situations. While keeping the existing methods of WaitGroup, can we add a
Go(func())
function to make it more seamless? (And while at it, we could even make theGo
method return theWaitGroup
itself for chained calls? I wouldn't insist on this but it would be nice instead of it returning nothing)It will be like:
vs
or potentially:
I feel like it's not a crazy far-fetched idea since errgroup.Group already has this method.
And this would be a nice addition forsemaphore.Weighted
as well, what do you think?The text was updated successfully, but these errors were encountered: