mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-19 09:02:39 +08:00
maps
This commit is contained in:
28
maps/map.go
28
maps/map.go
@ -3,13 +3,16 @@ package maps
|
||||
import "golang.org/x/exp/constraints"
|
||||
|
||||
type Map[K constraints.Ordered, V any] interface {
|
||||
Set(key K, value V)
|
||||
Get(key K) (value V, ok bool)
|
||||
Exist(key K) bool
|
||||
Delete(key K)
|
||||
Clone() Map[K, V]
|
||||
Clear()
|
||||
Count() int
|
||||
Set(key K, value V) // 设置值
|
||||
Get(key K) (value V, ok bool) // 获取值
|
||||
Exist(key K) bool // 键是否存在
|
||||
Delete(key K) // 删除值
|
||||
Keys() []K // 获取所有键
|
||||
Values() []V // 获取所有值
|
||||
ToMap() map[K]V // 转换为map
|
||||
Clone() Map[K, V] // 复制
|
||||
Clear() // 清空
|
||||
Count() int // 数量
|
||||
Iter() <-chan *Entry[K, V]
|
||||
ForEach(f func(K, V))
|
||||
}
|
||||
@ -18,3 +21,14 @@ type Entry[K constraints.Ordered, V any] struct {
|
||||
Key K
|
||||
Value V
|
||||
}
|
||||
|
||||
func Merge[K comparable, V any](mm ...map[K]V) map[K]V {
|
||||
ret := make(map[K]V)
|
||||
for _, m := range mm {
|
||||
for k, v := range m {
|
||||
ret[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
Reference in New Issue
Block a user