-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (45 loc) · 1 KB
/
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
// Copyright (c) 2023 Bruno Marques Venceslau de Souza. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"sync"
"time"
"github.com/brunomvsouza/singleflight"
)
func main() {
var (
group singleflight.Group[string, string]
wg sync.WaitGroup
)
wg.Add(2)
go func() {
defer wg.Done()
res1, err1, shared1 := group.Do("key", func() (string, error) {
time.Sleep(10 * time.Millisecond)
return "func 1", nil
})
fmt.Println("res1:", res1)
fmt.Println("err1:", err1)
fmt.Println("shared1:", shared1)
}()
go func() {
defer wg.Done()
res2, err2, shared2 := group.Do("key", func() (string, error) {
time.Sleep(10 * time.Millisecond)
return "func 2", nil
})
fmt.Println("res2:", res2)
fmt.Println("err2:", err2)
fmt.Println("shared2:", shared2)
}()
wg.Wait()
// Output:
// res1: func 2
// err1: <nil>
// res2: func 2
// err2: <nil>
// shared2: true
// shared1: true
}