-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (49 loc) · 918 Bytes
/
main.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
package main
import (
"errors"
"fmt"
"strconv"
"sync"
"time"
"golang.org/x/sync/singleflight"
)
var group singleflight.Group
func main() {
client("hoge")
clientSingle("hoge")
fmt.Println("===== goroutine START =====")
var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
client("hoge")
clientSingle("hoge")
wg.Done()
}()
}
wg.Wait()
}
func clientSingle(param string) (int, error) {
// param string only
v, err, _ := group.Do(param, func() (interface{}, error) {
time.Sleep(time.Second * 1)
fmt.Println("call single!")
if param != "" {
return "1", nil
}
return nil, errors.New("hoge")
})
if err != nil {
return 0, err
}
i, err := strconv.Atoi(v.(string))
return i, err
}
func client(param string) (int, error) {
time.Sleep(time.Second * 1)
fmt.Println("call Not single!")
if param != "" {
return 2, nil
}
return 0, errors.New("hoge")
}