Skip to content

Commit fa8dd60

Browse files
committed
add map
1 parent 1cbe142 commit fa8dd60

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

map.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package handy
2+
3+
func NewMap[TK comparable, TV any]() *Map[TK, TV] {
4+
return &Map[TK, TV]{
5+
m: make(map[TK]TV),
6+
}
7+
}
8+
9+
type Map[TK comparable, TV any] struct {
10+
m map[TK]TV
11+
}
12+
13+
func (m *Map[TK, TV]) HasKey(key TK) bool {
14+
_, ok := m.m[key]
15+
return ok
16+
}
17+
18+
func (m *Map[TK, TV]) Set(key TK, value TV) {
19+
m.m[key] = value
20+
}
21+
22+
func (m *Map[TK, TV]) SetOnce(key TK, value TV) bool {
23+
if _, ok := m.m[key]; !ok {
24+
m.m[key] = value
25+
return true
26+
}
27+
return false
28+
}
29+
30+
func (m *Map[TK, TV]) Merge(maps ...*Map[TK, TV]) {
31+
for _, each := range maps {
32+
for k, v := range each.m {
33+
m.m[k] = v
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)