Documentation ¶
Index ¶
- type Map
- func (m *Map[K, V]) CopyData() map[K]V
- func (m *Map[K, V]) Delete(key K)
- func (m *Map[K, V]) DeleteWithFunc(key K, onDeleteFunc func(value V))
- func (m *Map[K, V]) Length() int
- func (m *Map[K, V]) Load(key K) (V, bool)
- func (m *Map[K, V]) LoadAndDelete(key K) (V, bool)
- func (m *Map[K, V]) LoadAndDeleteAll() map[K]V
- func (m *Map[K, V]) LoadAndDeleteWithFunc(key K, onLoadFunc func(value V) V) (V, bool)
- func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)
- func (m *Map[K, V]) LoadOrStoreWithFunc(key K, onLoadFunc func(value V) V, createFunc func() V) (actual V, loaded bool)
- func (m *Map[K, V]) LoadWithFunc(key K, onLoadFunc func(value V) V) (V, bool)
- func (m *Map[K, V]) Range(f func(key K, value V) bool)
- func (m *Map[K, V]) Range2(f func(key K, value V) bool)
- func (m *Map[K, V]) Replace(key K, value V) (oldValue V, oldLoaded bool)
- func (m *Map[K, V]) ReplaceWithFunc(key K, ...) (oldValue V, oldLoaded bool)
- func (m *Map[K, V]) Store(key K, value V)
- func (m *Map[K, V]) StoreWithFunc(key K, createFunc func() V)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map is like a Go map[interface{}]interface{} but is safe for concurrent use by multiple goroutines.
func (*Map[K, V]) CopyData ¶
func (m *Map[K, V]) CopyData() map[K]V
CopyData creates a deep copy of the internal map.
func (*Map[K, V]) DeleteWithFunc ¶
func (m *Map[K, V]) DeleteWithFunc(key K, onDeleteFunc func(value V))
DeleteWithFunc removes the key from the map and if a value existed invokes the onDeleteFunc callback on the removed value.
The onDeleteFunc callback is invoked under a write lock.
func (*Map[K, V]) Load ¶
Load returns the value stored in the map for a key, or nil if no value is present. The loaded value is read-only and should not be modified. The ok result indicates whether value was found in the map.
func (*Map[K, V]) LoadAndDelete ¶
LoadAndDelete loads and deletes the value for the key.
func (*Map[K, V]) LoadAndDeleteAll ¶
func (m *Map[K, V]) LoadAndDeleteAll() map[K]V
LoadAndDelete loads and deletes the value for the key.
func (*Map[K, V]) LoadAndDeleteWithFunc ¶
LoadAndDeleteWithFunc removes the key from the map and if a value existed invokes the onLoadFunc callback on the removed and return it.
The onLoadFunc callback is invoked under a write lock.
func (*Map[K, V]) LoadOrStore ¶
LoadOrStore returns the existing value for the key if present. The loaded value is read-only and should not be modified. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.
func (*Map[K, V]) LoadOrStoreWithFunc ¶
func (m *Map[K, V]) LoadOrStoreWithFunc(key K, onLoadFunc func(value V) V, createFunc func() V) (actual V, loaded bool)
LoadOrStoreWithFunc loads an existing element from the map or creates a new element and stores it in the map
The onLoadFunc or createFunc are invoked under a write lock.
func (*Map[K, V]) LoadWithFunc ¶
LoadWithFunc tries to load element for key from the map, if it exists then the onload functions is invoked on it.
The onLoadFunc is invoked under a read lock.
func (*Map[K, V]) Range ¶
Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.
Range does not copy the whole map, instead the read lock is locked on iteration of the map, and unlocked before f is called.
func (*Map[K, V]) Range2 ¶
Range2 calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.
Range2 differs from Range by keepting a read lock locked during the whole call.
func (*Map[K, V]) Replace ¶
Replace replaces the existing value with a new value and returns old value for the key.
func (*Map[K, V]) ReplaceWithFunc ¶
func (m *Map[K, V]) ReplaceWithFunc(key K, onReplaceFunc func(oldValue V, oldLoaded bool) (newValue V, doDelete bool)) (oldValue V, oldLoaded bool)
ReplaceWithFunc checks whether key exists in the map, invokes the onReplaceFunc callback on the pair (value, ok) and either deletes or stores the element in the map based on the returned values from the onReplaceFunc callback.
The onReplaceFunc callback is invoked under a write lock.
func (*Map[K, V]) StoreWithFunc ¶
func (m *Map[K, V]) StoreWithFunc(key K, createFunc func() V)
StoreWithFunc creates a new element and stores it in the map under the given key.
The createFunc is invoked under a write lock.