Documentation
¶
Index ¶
- type GenericSyncMap
- func (gsm *GenericSyncMap[K, V]) Clear()
- func (gsm *GenericSyncMap[K, V]) CompareAndDelete(key K, old V) (deleted bool)
- func (gsm *GenericSyncMap[K, V]) CompareAndSwap(key K, old V, new V) (swapped bool)
- func (gsm *GenericSyncMap[K, V]) Delete(key K)
- func (gsm *GenericSyncMap[K, V]) Keys() iter.Seq[K]
- func (gsm *GenericSyncMap[K, V]) Load(key K) (value V, ok bool)
- func (gsm *GenericSyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool)
- func (gsm *GenericSyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)
- func (gsm *GenericSyncMap[K, V]) Range() iter.Seq2[K, V]
- func (gsm *GenericSyncMap[K, V]) Store(key K, value V)
- func (gsm *GenericSyncMap[K, V]) Swap(key K, value V) (previous V, loaded bool)
- func (gsm *GenericSyncMap[K, V]) Values() iter.Seq[V]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type GenericSyncMap ¶
type GenericSyncMap[K comparable, V any] struct { // contains filtered or unexported fields }
GenericSyncMap is like sync.Map with generic key and value types K and V. The zero GenericSyncMap is empty and ready for use. A GenericSyncMap must not be copied after first use.
Example ¶
package main import ( "fmt" "slices" "github.com/aaronriekenberg/gsm" ) func main() { type name string type person struct { title string age int } nameToPerson := gsm.GenericSyncMap[name, person]{} nameToPerson.Store("alice", person{title: "engineer", age: 25}) nameToPerson.Store("bob", person{title: "manager", age: 35}) value, ok := nameToPerson.Load("alice") fmt.Printf("alice value = %+v ok = %v\n", value, ok) fmt.Printf("alice title = %q\n", value.title) value, ok = nameToPerson.Load("bob") fmt.Printf("bob value = %+v ok = %v\n", value, ok) fmt.Printf("bob age = %v\n", value.age) swapped := nameToPerson.CompareAndSwap( "alice", person{title: "engineer", age: 25}, person{title: "manager", age: 25}, ) value, ok = nameToPerson.Load("alice") fmt.Printf("swapped = %v value = %+v ok = %v\n", swapped, value, ok) keys := slices.Sorted(nameToPerson.Keys()) fmt.Printf("keys = %v", keys) }
Output: alice value = {title:engineer age:25} ok = true alice title = "engineer" bob value = {title:manager age:35} ok = true bob age = 35 swapped = true value = {title:manager age:25} ok = true keys = [alice bob]
func (*GenericSyncMap[K, V]) Clear ¶
func (gsm *GenericSyncMap[K, V]) Clear()
Clear deletes all the entries, resulting in an empty Map.
func (*GenericSyncMap[K, V]) CompareAndDelete ¶
func (gsm *GenericSyncMap[K, V]) CompareAndDelete( key K, old V, ) (deleted bool)
CompareAndDelete deletes the entry for key if its value is equal to old. The old value must be of a comparable type.
If there is no current value for key in the map, CompareAndDelete returns false (even if the old value is the nil interface value).
func (*GenericSyncMap[K, V]) CompareAndSwap ¶
func (gsm *GenericSyncMap[K, V]) CompareAndSwap( key K, old V, new V, ) (swapped bool)
CompareAndSwap swaps the old and new values for key if the value stored in the map is equal to old. The old value must be of a comparable type.
func (*GenericSyncMap[K, V]) Delete ¶
func (gsm *GenericSyncMap[K, V]) Delete( key K, )
Delete deletes the value for a key.
func (*GenericSyncMap[K, V]) Keys ¶
func (gsm *GenericSyncMap[K, V]) Keys() iter.Seq[K]
Range returns an iter.Seq[K] over all keys in the map.
func (*GenericSyncMap[K, V]) Load ¶
func (gsm *GenericSyncMap[K, V]) Load( key K, ) (value V, ok bool)
Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.
func (*GenericSyncMap[K, V]) LoadAndDelete ¶
func (gsm *GenericSyncMap[K, V]) LoadAndDelete( key K, ) (value V, loaded bool)
LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.
func (*GenericSyncMap[K, V]) LoadOrStore ¶
func (gsm *GenericSyncMap[K, V]) LoadOrStore( key K, value V, ) (actual V, loaded bool)
LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.
func (*GenericSyncMap[K, V]) Range ¶
func (gsm *GenericSyncMap[K, V]) Range() iter.Seq2[K, V]
Range returns an iter.Seq2[K, V] over all entries in the map.
func (*GenericSyncMap[K, V]) Store ¶
func (gsm *GenericSyncMap[K, V]) Store( key K, value V, )
Store sets the value for a key.
func (*GenericSyncMap[K, V]) Swap ¶
func (gsm *GenericSyncMap[K, V]) Swap( key K, value V, ) (previous V, loaded bool)
Swap swaps the value for a key and returns the previous value if any. The loaded result reports whether the key was present.
func (*GenericSyncMap[K, V]) Values ¶
func (gsm *GenericSyncMap[K, V]) Values() iter.Seq[V]
Range returns an iter.Seq[V] over all values in the map.