Created
July 23, 2020 02:31
-
-
Save icexin/dda98e3c7dbbf2d8b37ef1fc79267197 to your computer and use it in GitHub Desktop.
example of errgroup
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
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