Documentation
¶
Overview ¶
Package weakutil provides utilities for the weak package.
Index ¶
- type Map
- func (m *Map[K, V]) All() iter.Seq2[K, *V]
- func (m *Map[K, V]) Clear()
- func (m *Map[K, V]) CompareAndDelete(key K, old *V) (deleted bool)
- func (m *Map[K, V]) CompareAndSwap(key K, oldValue, newValue *V) (swapped bool)
- func (m *Map[K, V]) Delete(key K)
- func (m *Map[K, V]) Load(key K) (value *V, ok bool)
- func (m *Map[K, V]) LoadAndDelete(key K) (value *V, loaded bool)
- func (m *Map[K, V]) LoadOrStore(key K, value *V) (actual *V, loaded bool)
- func (m *Map[K, V]) Range(f func(key K, value *V) bool)
- func (m *Map[K, V]) Store(key K, value *V)
- func (m *Map[K, V]) Swap(key K, value *V) (previous *V, loaded bool)
Examples ¶
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 a map that stores weak.Pointer. Values are automatically evicted when they are no longer reachable. It is safe for concurrent use. The zero value is ready to use.
It implements the same methods as sync.Map.
Example ¶
m := new(Map[string, [64]byte])
v := &[64]byte{} // Must use a large value in order to trigger garbage collection reliably.
m.Store("test", v)
runtime.GC()
fmt.Println(m.Load("test")) // The pointer is still valid, because there is a keepalive below.
runtime.KeepAlive(v)
runtime.GC()
fmt.Println(m.Load("test")) // The pointer is not valid anymore.
Output: &[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] true <nil> false
func (*Map[K, V]) All ¶ added in v0.14.0
All returns an iterator over all entries in the map. See Map.Range for more details.
func (*Map[K, V]) CompareAndDelete ¶
CompareAndDelete is like sync.Map.CompareAndDelete.
func (*Map[K, V]) CompareAndSwap ¶
CompareAndSwap is like sync.Map.CompareAndSwap.
func (*Map[K, V]) Load ¶
Load is like sync.Map.Load.
func (*Map[K, V]) LoadAndDelete ¶
LoadAndDelete is like sync.Map.LoadAndDelete.
func (*Map[K, V]) LoadOrStore ¶
LoadOrStore is like sync.Map.LoadOrStore.
func (*Map[K, V]) Range ¶
Range is like sync.Map.Range.
func (*Map[K, V]) Swap ¶
Swap is like sync.Map.Swap.