Last active
September 27, 2018 19:31
-
-
Save comtom/96447325ccd5c5b00fe0d2ffb4fba8a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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