-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
77 lines (68 loc) · 1.3 KB
/
examples_test.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package goroutine_test
import (
"fmt"
"github.com/sknr/goroutine"
)
func ExampleGo() {
// Instead of
go func() {
values := [3]int{1, 2, 3}
for i := 0; i < 4; i++ {
fmt.Println(values[i])
}
}()
// simply call
goroutine.Go(func() {
values := [3]int{1, 2, 3}
for i := 0; i < 4; i++ {
fmt.Println(values[i])
}
})
}
func ExampleGo_withInputParam() {
// Functions with input params need to be wrapped by an anonymous function.
// Instead of
go func(s string) {
panic(s)
}("Hello World")
// simply call
goroutine.Go(func() {
func(s string) {
panic(s)
}("Hello World")
})
}
func ExampleNew() {
err := <-goroutine.New(func() {
values := [3]int{1, 2, 3}
for i := 0; i < 4; i++ {
fmt.Println(values[i])
}
}).Go()
fmt.Println(err)
// Output:
// 1
// 2
// 3
// panic in goroutine recovered: runtime error: index out of range [3] with length 3
}
func ExampleGoroutine_WithRecover() {
err := <-goroutine.New(func() {
values := [3]int{1, 2, 3}
for i := 0; i < 4; i++ {
fmt.Println(values[i])
}
}).WithRecover(func(v interface{}, done chan<- error) {
if err, ok := v.(error); ok {
done <- err
return
}
done <- fmt.Errorf("recovered: %v", v)
}).Go()
fmt.Println(err)
// Output:
// 1
// 2
// 3
// runtime error: index out of range [3] with length 3
}