Skip to content

Instantly share code, notes, and snippets.

@icexin
Created July 23, 2020 02:31
Show Gist options
  • Save icexin/dda98e3c7dbbf2d8b37ef1fc79267197 to your computer and use it in GitHub Desktop.
Save icexin/dda98e3c7dbbf2d8b37ef1fc79267197 to your computer and use it in GitHub Desktop.
example of errgroup
package main
import "golang.org/x/sync/errgroup"
func doData1() (string, error)
func doData2() (string, error)
func doData3() (string, error)
func main() {
var data1, data2, data3 string
g := new(errgroup.Group)
g.Go(func() error {
var err error
data1, err = doData1()
return err
})
g.Go(func() error {
var err error
data2, err = doData2()
return err
})
g.Go(func() error {
var err error
data3, err = doData3()
return err
})
err := g.Wait()
if err != nil {
panic(err)
}
// do something with data1 data2 data3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment