Documentation ¶
Overview ¶
Package maps 提供原生 map 类型相关的一些工具
Index ¶
- func Equal[K comparable, V comparable](a, b map[K]V) bool
- func GetOrDefault[K constraints.Ordered, V any](m map[K]V, key K, defaultValue V) V
- func GetStringOrFormat[K constraints.Ordered, V ~string](m map[K]V, key K, format string) V
- func Keys[K constraints.Ordered, V any](m map[K]V) (keys slice.List[K])
- func KeysEqual[K comparable, V1, V2 any](a map[K]V1, b map[K]V2) bool
- func Values[K constraints.Ordered, V any](m map[K]V) (values []V)
- type KVPair
- type RWSafeMap
- type Set
- type SortOrder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetOrDefault ¶
func GetOrDefault[K constraints.Ordered, V any](m map[K]V, key K, defaultValue V) V
GetOrDefault 从 map 中获取数据, 如果不存在则返回 default value
func GetStringOrFormat ¶
func GetStringOrFormat[K constraints.Ordered, V ~string](m map[K]V, key K, format string) V
GetStringOrFormat 从 value 为 string 的 map 中获取数据, 如果不存在则使用 fmt.Sprintf(format, key) 的测试返回 value
func KeysEqual ¶
func KeysEqual[K comparable, V1, V2 any](a map[K]V1, b map[K]V2) bool
KeysEqual 判断两个 map 是否拥有相同的 keys
Types ¶
type KVPair ¶
type KVPair[K comparable, V any] struct { K K V V }
KVPair 表示键值对, 便于调用方排序用。
func KeyValuesAndSortByKeys ¶
func KeyValuesAndSortByKeys[K constraints.Ordered, V any](m map[K]V, sortOrder SortOrder) []KVPair[K, V]
KeyValuesAndSortByKeys 获取键值对列表, 并且按照 key 排序
func KeyValuesAndSortByValues ¶
func KeyValuesAndSortByValues[K comparable, V constraints.Ordered](m map[K]V, sortOrder SortOrder) []KVPair[K, V]
KeyValuesAndSortByValues 获取键值对列表, 并且按照 key 排序
type RWSafeMap ¶
type RWSafeMap[K comparable, V any] interface { json.Marshaler json.Unmarshaler Store(k K, v V) Load(k K) (V, bool) Delete(k K) LoadAndDelete(k K) (value V, loaded bool) LoadOrStore(k K, v V) (actual V, loaded bool) LoadOrNew(k K, newFunc func() V) (actual V, loaded bool) Swap(k K, v V) (previous V, loaded bool) Range(f func(key K, value V) bool) Size() int }
RWSafeMap 即 Read-Write-Locked Safe Map, 表示这是一个使用读写锁锁住的安全的 map 类型。 适用于读多写少的情况。
func NewRWSafeMap ¶
func NewRWSafeMap[K comparable, V any](capacity ...int) RWSafeMap[K, V]
NewRWSafeMap 新建一个 RWSafeMap 实例。可选参数只有一个, 表示 capacity
type Set ¶
type Set[K comparable] map[K]struct{}
Set 表示一个集合
func NewSetFromSlice ¶
func NewSetFromSlice[T constraints.Ordered](sli slice.List[T]) Set[T]
NewSetFromSlice 从一个切片转为 Set 类型
func NewSetWithCapacity ¶
func NewSetWithCapacity[K comparable, I constraints.Integer](cap I) Set[K]
NewSetWithCapacity 返回一个集合类型并初始化容量
Click to show internal directories.
Click to hide internal directories.