Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created May 17, 2012 20:46
Show Gist options
  • Select an option

  • Save jordanorelli/2721495 to your computer and use it in GitHub Desktop.

Select an option

Save jordanorelli/2721495 to your computer and use it in GitHub Desktop.
paramlist := map[string]params{
"one": &params{ ... },
"two": &params{ ... },
"three": &params{ ... },
}
results := make(map[string]result, len(paramlist))
for k, v := range paramlist {
wg.Add()
go func(label string, myparams param) {
defer wg.Done()
// something that causes a result to be created
results[label] = result
}(k, v)
}
wg.Wait()
// at this point, results is a complete map[string]result
c := make(chan resultEnvelope)
paramlist := map[string]params{
"one": &params{ ... },
"two": &params{ ... },
"three": &params{ ... },
}
for k, v := range paramlist {
go func(label string, myparams param) {
// something that causes a result to be created
c <- resultEnvelope{Key: label, Value: result}
}(k, v)
}
results := make(map[string]result, len(paramlist))
for _, envelope := range c {
results[envelope.Key] = envelope.Value
if received == len(paramlist) {
break
}
}
// at this point, results is a complete map[string]result.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment