-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapsafe.go
55 lines (47 loc) · 988 Bytes
/
mapsafe.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
package shoset
import "sync"
// MapSafe : simple key map safe for goroutines...
type MapSafe struct {
m map[string]interface{}
sync.Mutex
}
// NewMapSafe : constructor
func NewMapSafe() *MapSafe {
m := new(MapSafe)
m.m = make(map[string]interface{})
return m
}
// Get : Get a value from a MapSafe
func (m *MapSafe) Get(key string) interface{} {
m.Lock()
defer m.Unlock()
return m.m[key]
}
// Set : assign a value to a MapSafe
func (m *MapSafe) Set(key string, value interface{}) *MapSafe {
m.Lock()
m.m[key] = value
m.Unlock()
return m
}
// Delete : delete a value in a MapSafe
func (m *MapSafe) Delete(key string) {
m.Lock()
_, ok := m.m[key]
if ok {
delete(m.m, key)
}
m.Unlock()
}
// Iterate : iterate through MapSafe Values using a function
func (m *MapSafe) Iterate(iter func(string, interface{})) {
m.Lock()
for key, val := range m.m {
iter(key, val)
}
m.Unlock()
}
// Len : return length of the map
func (m *MapSafe) Len() int {
return len(m.m)
}