-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
59 lines (48 loc) · 1.08 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
55
56
57
58
59
package main
import (
"fmt"
"sync"
)
func logCounter() {
var counter sync.Map
var wg sync.WaitGroup
logs := []string{"INFO", "WARNING", "DEBUG", "ERROR", "FATAL", "DEBUG", "DEBUG"}
recordLog := func (logType string) {
value, _ := counter.LoadOrStore(logType, 0)
count := value.(int)
counter.Store(logType, count + 1)
}
for _, v := range logs {
wg.Add(1)
go func (k string) {
defer wg.Done()
recordLog(k)
}(v)
}
wg.Wait()
counter.Range(func(key, value any) bool {
fmt.Printf("Log Type: %s, Count: %d\n", key, value)
return true
})
}
func expensiveCache() {
var computationCache sync.Map
expensiveComputation := func (input int) int {
return input * input
}
getResult := func (input int) int {
if value, ok := computationCache.Load(input); ok {
return value.(int)
}
result := expensiveComputation(input)
computationCache.Store(input, result)
return result
}
fmt.Println("Result 1:", getResult(9999))
fmt.Println("Result 2:", getResult(9999))
fmt.Println("Result 3:", getResult(9))
}
func main() {
logCounter()
expensiveCache()
}