Repository navigation
Expand file tree
/
Copy pathup.go
More file actions
94 lines (83 loc) · 2.13 KB
/
Copy pathup.go
File metadata and controls
94 lines (83 loc) · 2.13 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package syncmap
import (
"fmt"
)
/*
Provides utilities to make usage simple. Supports getting items, values, and data exchange between maps.
提供辅助工具,简化使用。支持获取项、值以及在 map 间交换数据等操作。
*/
// GetMap returns each item as a standard Go map.
// GetMap 将每一项作为标准 Go map 返回。
func (m *Map[K, V]) GetMap() map[K]V {
res := make(map[K]V)
m.Range(func(k K, v V) bool {
res[k] = v
return true
})
return res
}
// SetMap adds and updates items from a standard Go map.
// SetMap 从标准 Go map 添加和更新项到 Map。
func (m *Map[K, V]) SetMap(mp map[K]V) {
for k, v := range mp {
m.Store(k, v)
}
}
// SetSyncMap copies each item from a Map.
// SetSyncMap 从一个 Map 复制每一项到当前 Map。
func (m *Map[K, V]) SetSyncMap(mp *Map[K, V]) {
mp.Range(func(k K, v V) bool {
m.Store(k, v)
return true
})
}
// SetSyncMaps copies each item from multiple Maps.
// SetSyncMaps 从多个 Map 复制每一项到当前 Map。
func (m *Map[K, V]) SetSyncMaps(mps ...*Map[K, V]) {
for _, mp := range mps {
m.SetSyncMap(mp)
}
}
// Debug prints each item to help with debugging.
// Debug 打印每一项,用于调试。
func (m *Map[K, V]) Debug() {
fmt.Println("-----------")
m.Range(func(k K, v V) bool {
fmt.Println(k, v)
return true
})
fmt.Println("-----------")
}
// Has checks if an item exists in the Map.
// Has 检查项是否存在于 Map 中。
func (m *Map[K, V]) Has(key K) bool {
_, ok := m.Load(key)
return ok
}
// Count returns the count of items in the Map.
// Count 返回 Map 中的项数量。
func (m *Map[K, V]) Count() (size int) {
m.Range(func(k K, v V) bool {
size++
return true
})
return size
}
// Keys gets each item's name from the Map.
// Keys 获取 Map 中每项的名称。
func (m *Map[K, V]) Keys() (keys []K) {
m.Range(func(k K, v V) bool {
keys = append(keys, k)
return true
})
return keys
}
// Values gets each item's content from the Map.
// Values 获取 Map 中每项的内容。
func (m *Map[K, V]) Values() (values []V) {
m.Range(func(k K, v V) bool {
values = append(values, v)
return true
})
return values
}