-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
43 lines (35 loc) · 778 Bytes
/
Copy pathexample_test.go
File metadata and controls
43 lines (35 loc) · 778 Bytes
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
package errgroup_test
import (
"fmt"
"github.com/min0625/errgroup"
)
func Example() {
// This case uses "github.com/min0625/errgroup" which will catch panics.
// If you import "golang.org/x/sync/errgroup" instead, it won't catch panics.
// You can try this in the Go Playground: https://go.dev/play/p/7pUX6uQ2mCH
var g errgroup.Group
defer func() {
// Will catch the panic.
if p := recover(); p != nil {
switch t := p.(type) {
case errgroup.PanicValue:
fmt.Println(t.Recovered)
case errgroup.PanicError:
fmt.Println(t.Recovered)
}
}
}()
g.Go(func() error {
// Do something
return nil
})
g.Go(func() error {
panic("oops")
})
if err := g.Wait(); err != nil {
// Handle error
fmt.Println(err)
return
}
// Output: oops
}