weakutil

package
v0.25.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package weakutil provides utilities for the weak package.

Index

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

func (m *Map[K, V]) All() iter.Seq2[K, *V]

All returns an iterator over all entries in the map. See Map.Range for more details.

func (*Map[K, V]) Clear

func (m *Map[K, V]) Clear()

Clear is like sync.Map.Clear.

func (*Map[K, V]) CompareAndDelete

func (m *Map[K, V]) CompareAndDelete(key K, old *V) (deleted bool)

CompareAndDelete is like sync.Map.CompareAndDelete.

func (*Map[K, V]) CompareAndSwap

func (m *Map[K, V]) CompareAndSwap(key K, oldValue, newValue *V) (swapped bool)

CompareAndSwap is like sync.Map.CompareAndSwap.

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(key K)

Delete is like sync.Map.Delete.

func (*Map[K, V]) Load

func (m *Map[K, V]) Load(key K) (value *V, ok bool)

Load is like sync.Map.Load.

func (*Map[K, V]) LoadAndDelete

func (m *Map[K, V]) LoadAndDelete(key K) (value *V, loaded bool)

LoadAndDelete is like sync.Map.LoadAndDelete.

func (*Map[K, V]) LoadOrStore

func (m *Map[K, V]) LoadOrStore(key K, value *V) (actual *V, loaded bool)

LoadOrStore is like sync.Map.LoadOrStore.

func (*Map[K, V]) Range

func (m *Map[K, V]) Range(f func(key K, value *V) bool)

Range is like sync.Map.Range.

func (*Map[K, V]) Store

func (m *Map[K, V]) Store(key K, value *V)

Store is like sync.Map.Store.

func (*Map[K, V]) Swap

func (m *Map[K, V]) Swap(key K, value *V) (previous *V, loaded bool)

Swap is like sync.Map.Swap.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL