-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapx.go
25 lines (22 loc) · 933 Bytes
/
mapx.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
package mapx
// Mapx
// generic type map which is thread safe or not
type Mapx[Key comparable, Value any] interface {
Get(Key) (Value, bool) // Get value by key.
Set(Key, Value) // Set value to key.
Delete(Key) // Delete by key.
Len() int // Get length.
Range(fn func(Key, Value)) // Range.
Keys() []Key // Get keys.
}
// New[Key comparable, Value any](threadSafe bool) returns a new Mapx[Key, Value] based on the value of threadSafe.
//
// threadSafe: a boolean indicating whether the mapx should be thread safe or not.
// Returns: an instance of Mapx[Key, Value], either a thread safe or non-thread safe depending on the value of threadSafe.
func New[Key comparable, Value any](threadSafe bool) Mapx[Key, Value] {
if threadSafe {
return newSafeMapx[Key, Value]() // 线程安全mapx
} else {
return newUnSafeMapx[Key, Value]() // 非线程安全mapx
}
}