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