-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
66 lines (58 loc) · 993 Bytes
/
string.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
package set
import "sync"
type void struct{}
// String struct for dync map
type String struct {
d map[string]void
m sync.RWMutex
}
// Load for load data by key provided
func (m *String) Load(key string) bool {
if key == "" {
return false
}
m.m.RLock()
defer m.m.RUnlock()
if m.d == nil {
return false
}
_, ok := m.d[key]
return ok
}
// Store for load data by key provided
func (m *String) Store(key string) {
if key == "" {
return
}
m.m.Lock()
defer m.m.Unlock()
if m.d == nil {
m.d = map[string]void{}
}
m.d[key] = void{}
}
// Delete for delete data by key provided
func (m *String) Delete(key string) {
if key == "" {
return
}
m.m.Lock()
defer m.m.Unlock()
if m.d == nil {
return
}
delete(m.d, key)
}
// Keys for return all keys in set
func (m *String) Keys() []string {
m.m.RLock()
defer m.m.RUnlock()
if m.d == nil {
return []string{}
}
keys := make([]string, 0, len(m.d))
for k := range m.d {
keys = append(keys, k)
}
return keys
}