Skip to content

Instantly share code, notes, and snippets.

@collinvandyck
Created August 12, 2016 15:54
Show Gist options
  • Save collinvandyck/2409eed309e6af302bc0f1aca1857990 to your computer and use it in GitHub Desktop.
Save collinvandyck/2409eed309e6af302bc0f1aca1857990 to your computer and use it in GitHub Desktop.
package models
type concurrent struct {
fns []func() (interface{}, error)
}
func (this *concurrent) addFn(fn func() (interface{}, error)) *concurrent {
this.fns = append(this.fns, fn)
return this
}
func (this *concurrent) run() []resultAndError {
var result []resultAndError
ch := make(chan resultAndError)
for _, fn := range this.fns {
delegate := fn
go func() {
res, err := delegate()
ch <- resultAndError{result:res, err:err}
}()
}
for _ = range this.fns {
result = append(result, <-ch)
}
return result
}
@kenkeiter
Copy link

To answer your question: Yes, you should. 😘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment