-
Notifications
You must be signed in to change notification settings - Fork 19
/
amap.go
57 lines (47 loc) · 896 Bytes
/
amap.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
package sshego
import (
"fmt"
"sync"
)
// atomic map from string to *User
//go:generate greenpack
type AtomicUserMap struct {
U map[string]*User
tex sync.RWMutex
}
func NewAtomicUserMap() *AtomicUserMap {
return &AtomicUserMap{
U: make(map[string]*User),
}
}
func (m *AtomicUserMap) Get(key string) *User {
m.tex.RLock()
defer m.tex.RUnlock()
return m.U[key]
}
func (m *AtomicUserMap) Get2(key string) (*User, bool) {
m.tex.RLock()
defer m.tex.RUnlock()
v, ok := m.U[key]
return v, ok
}
func (m *AtomicUserMap) Set(key string, val *User) {
m.tex.Lock()
defer m.tex.Unlock()
m.U[key] = val
}
func (m *AtomicUserMap) Del(key string) {
m.tex.Lock()
defer m.tex.Unlock()
delete(m.U, key)
}
func (m *AtomicUserMap) String() string {
m.tex.Lock()
defer m.tex.Unlock()
s := "{"
for k, v := range m.U {
s += fmt.Sprintf(`"%s":%s,\n`, k, v)
}
s += "}"
return s
}