-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrgorup-basics-2.go
46 lines (34 loc) · 941 Bytes
/
errgorup-basics-2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"fmt"
"golang.org/x/sync/errgroup"
)
type validationError string
func (v validationError) Error() string {
return string(v)
}
const validationError1 = validationError("Some validation error 1")
const validationError3 = validationError("Some validation error 3")
/*
use group.Wait() to wait for all tasks in the group to complete.
If any of the tasks finish with an error, the group.Wait() method will return the first error it received.
If all tasks complete successfully, the group.Wait() method will return nil.
*/
func egrpPattern2() {
egroup := errgroup.Group{}
egroup.Go(func() error {
fmt.Println("perform some task 1")
return validationError1
})
egroup.Go(func() error {
fmt.Println("perform some task 2")
return nil
})
egroup.Go(func() error {
fmt.Println("perform some task 3")
return validationError3
})
if err := egroup.Wait(); err != nil {
fmt.Println(err.Error())
}
}