-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
69 lines (55 loc) · 1.38 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
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"sync"
)
func main() {
regularMapUsage()
syncMapUsage()
}
func regularMapUsage() {
fmt.Println("Regular threatsafe map test")
fmt.Println("---------------------------")
// Create the threadsafe map.
reg := NewRegularStringMap()
// Fetch an item that doesn't exist yet.
result, ok := reg.Load("hello")
if ok {
fmt.Println(result)
} else {
fmt.Println("value not found for key: `hello`")
}
// Store an item in the map.
reg.Store("hello", "world")
fmt.Println("added value: `world` for key: `hello`")
// Fetch the item we just stored.
result, ok = reg.Load("hello")
if ok {
fmt.Printf("result: `%s` found for key: `hello`\n", result)
}
fmt.Println("---------------------------")
fmt.Println()
fmt.Println()
}
func syncMapUsage() {
fmt.Println("sync.Map test (Go 1.9+ only)")
fmt.Println("----------------------------")
// Create the threadsafe map.
var sm sync.Map
// Fetch an item that doesn't exist yet.
result, ok := sm.Load("hello")
if ok {
fmt.Println(result)
} else {
fmt.Println("value not found for key: `hello`")
}
// Store an item in the map.
sm.Store("hello", "world")
fmt.Println("added value: `world` for key: `hello`")
// Fetch the item we just stored.
result, ok = sm.Load("hello")
if ok {
fmt.Printf("result: `%s` found for key: `hello`\n", result.(string))
}
fmt.Println("---------------------------")
}