-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathmain.go
More file actions
35 lines (28 loc) · 624 Bytes
/
Copy pathmain.go
File metadata and controls
35 lines (28 loc) · 624 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
package main
import (
"fmt"
"sync"
"time"
)
func main() {
// sync.OnceValue (sync.OnceValues works the same)
onceString := sync.OnceValue[string](func() string {
time.Sleep(time.Second)
return "sync.OnceValue: computed once!"
})
fmt.Println(time.Now(), onceString())
fmt.Println(time.Now(), onceString())
fmt.Println(time.Now(), onceString())
// sync.OnceFunc
fmt.Println("")
onceFunc := sync.OnceFunc(func() {
time.Sleep(time.Second)
fmt.Println("sync.OnceFunc: computed once!")
})
fmt.Println(time.Now())
onceFunc()
fmt.Println(time.Now())
onceFunc()
fmt.Println(time.Now())
onceFunc()
}