Skip to content

Instantly share code, notes, and snippets.

@comtom
Last active September 27, 2018 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save comtom/96447325ccd5c5b00fe0d2ffb4fba8a9 to your computer and use it in GitHub Desktop.
Save comtom/96447325ccd5c5b00fe0d2ffb4fba8a9 to your computer and use it in GitHub Desktop.
import "golang.org/x/sync/errgroup"
...
g, ctx := errgroup.WithContext(ctx)
complete := make(chan []Store, len(locations))
for _, location := range locations {
g.Go(fetchStores(ctx, store.Id, complete))
}
if err := g.Wait(); err != nil {
log.Errorf("%s", err)
return nil, err
}
...
func fetchStores(ctx context.Context, storeId int, out chan<- []Store) func() error {
return func() error {
response, err := h.Client.Do(req)
if err != nil {
errorMsg := log.Errorf("Connection failed with error %v", err)
return nil, fmt.Errorf(errorMsg)
}
defer response.Body.Close()
out <- parseResponse(response)
return err
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment