Skip to content

Commit 8e87bbf

Browse files
committed
Modernize examples
1 parent d30ad8e commit 8e87bbf

2 files changed

Lines changed: 39 additions & 43 deletions

File tree

README.md

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,43 +38,43 @@ Here's a simple example of how to protect a function from concurrent calls.
3838
package main
3939

4040
import (
41-
"context"
42-
"fmt"
43-
"sync"
44-
"time"
41+
"context"
42+
"fmt"
43+
"sync"
44+
"time"
4545

46-
"resenje.org/singleflight"
46+
"resenje.org/singleflight"
4747
)
4848

4949
func main() {
50-
var g singleflight.Group[string, string]
51-
var wg sync.WaitGroup
52-
n := 5
53-
54-
// Make 5 concurrent calls for the same key.
55-
wg.Add(n)
56-
for i := 0; i < n; i++ {
57-
go func(i int) {
58-
defer wg.Done()
59-
60-
// The function will only be executed for the first call.
61-
// All other calls will wait and receive the same result.
62-
v, shared, err := g.Do(context.Background(), "user-profile", func(ctx context.Context) (string, error) {
63-
fmt.Println("Fetching user profile from database...")
64-
time.Sleep(100 * time.Millisecond) // Simulate slow DB query
65-
return "User data for John Doe", nil
66-
})
67-
68-
if err != nil {
69-
fmt.Printf("Goroutine %d: encountered an error: %v\n", i, err)
70-
return
71-
}
72-
73-
fmt.Printf("Goroutine %d: got result '%s' (shared: %t)\n", i, v, shared)
74-
}(i)
75-
}
76-
77-
wg.Wait()
50+
var g singleflight.Group[string, string]
51+
var wg sync.WaitGroup
52+
n := 5
53+
54+
// Make 5 concurrent calls for the same key.
55+
wg.Add(n)
56+
for i := range n {
57+
wg.Go(func() {
58+
defer wg.Done()
59+
60+
// The function will only be executed for the first call.
61+
// All other calls will wait and receive the same result.
62+
v, shared, err := g.Do(context.Background(), "user-profile", func(ctx context.Context) (string, error) {
63+
fmt.Println("Fetching user profile from database...")
64+
time.Sleep(100 * time.Millisecond) // Simulate slow DB query
65+
return "User data for John Doe", nil
66+
})
67+
68+
if err != nil {
69+
fmt.Printf("Goroutine %d: encountered an error: %v\n", i, err)
70+
return
71+
}
72+
73+
fmt.Printf("Goroutine %d: got result '%s' (shared: %t)\n", i, v, shared)
74+
})
75+
}
76+
77+
wg.Wait()
7878
}
7979
```
8080

examples_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,15 @@ func ExampleGroup_Do() {
3232
return "result", nil
3333
}
3434

35-
for i := 0; i < 10; i++ {
36-
wg.Add(1)
37-
go func() {
38-
defer wg.Done()
35+
for range 10 {
36+
wg.Go(func() {
3937
v, shared, err := g.Do(context.Background(), "key", fn)
4038
if err != nil {
4139
log.Printf("unexpected error: %v", err)
4240
return
4341
}
4442
fmt.Printf("Got result: %s, Shared: %t\n", v, shared)
45-
}()
43+
})
4644
}
4745

4846
wg.Wait()
@@ -88,10 +86,8 @@ func ExampleGroup_Do_panic() {
8886
panic("critical failure")
8987
}
9088

91-
for i := 0; i < 3; i++ {
92-
wg.Add(1)
93-
go func() {
94-
defer wg.Done()
89+
for range 3 {
90+
wg.Go(func() {
9591
defer func() {
9692
if r := recover(); r != nil {
9793
// Check if the recovered error contains the panic message and stack trace.
@@ -101,7 +97,7 @@ func ExampleGroup_Do_panic() {
10197
}
10298
}()
10399
_, _, _ = g.Do(context.Background(), "key", fn)
104-
}()
100+
})
105101
}
106102

107103
wg.Wait()

0 commit comments

Comments
 (0)