syncmap

package
v0.33.0 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: Apache-2.0 Imports: 2 Imported by: 3

Documentation

Overview

Package syncmap provides a typed thread-safe map implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SyncMap

type SyncMap[K comparable, V any] struct {
	sync.Mutex
	// contains filtered or unexported fields
}

SyncMap is a thread-safe map that uses Load and Store semantics. The zero value of SyncMap is an empty map ready to use. It must not be copied after first use.

func (*SyncMap[K, V]) Clone

func (s *SyncMap[K, V]) Clone() SyncMap[K, V]

Clone returns a clone of the SyncMap. The clone has a separate underlying map, but depending on how the values are stored, the values may be shared between the two maps. The new map has its own lock from the source.

func (*SyncMap[K, V]) Delete

func (s *SyncMap[K, V]) Delete(key K)

Delete deletes the value for a key.

func (*SyncMap[K, V]) Keys

func (s *SyncMap[K, V]) Keys() []K

Keys returns a slice of all keys in the map.

func (*SyncMap[K, V]) Load

func (s *SyncMap[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 (*SyncMap[K, V]) LoadOrStoreFunc added in v0.20.0

func (s *SyncMap[K, V]) LoadOrStoreFunc(key K, f func() (V, error)) (actual V, created bool, err error)

LoadOrStoreFunc returns the existing value for the key if present, otherwise it calls f and stores the result of f() in the map. If f returns an error, the value is not stored and the error is returned. The lock is held while calling f, so f must not block.

func (*SyncMap[K, V]) Range

func (s *SyncMap[K, V]) Range(f func(key K, value V) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration. The map is locked while calling f, so f must not block.

func (*SyncMap[K, V]) RemoveIf added in v0.19.0

func (s *SyncMap[K, V]) RemoveIf(f func(key K, value V) bool)

RemoveIf calls a function for all keys in the map. The syncmap is locked while calling the function, so it must not block. If the function returns true, the key is removed from the map. It is safe to "pluck" out the values from within the function.

func (*SyncMap[K, V]) Replace added in v0.10.8

func (s *SyncMap[K, V]) Replace(key K, value V) (previous V, ok bool)

Replace replaces the value for a key, and returns the previous value.

func (*SyncMap[K, V]) ReplaceFunc added in v0.20.0

func (s *SyncMap[K, V]) ReplaceFunc(key K, f func(current V) (V, error)) (actual V, replaced bool, err error)

ReplaceFunc calls f for a key and replaces the value with the result of f. If the key does not exist, the function is not called and the map is not modified. If the function returns an error, the value is not stored and the error is returned. The lock is held while calling f, so f must not block.

func (*SyncMap[K, V]) SetSize

func (s *SyncMap[K, V]) SetSize(size int)

SetSize ets the size of the map. This will pre-allocate the map to the specified size and must be called before any other operations or the call will be a no-op.

func (*SyncMap[K, V]) Store

func (s *SyncMap[K, V]) Store(key K, value V)

Store sets the value for a key.

func (*SyncMap[K, V]) Touch added in v0.11.2

func (s *SyncMap[K, V]) Touch(key K, f func(value V) V) (found bool)

Touch calls a function for a specific key. The syncmap is locked while calling the function, so it must not block. This allows a safe way to update a value in the map without needing to lock the object itself. If the key does not exist, the funciton is not called and the map is not modified.

func (*SyncMap[K, V]) Values

func (s *SyncMap[K, V]) Values() []V

Values returns a slice of all values in the map.

Jump to

Keyboard shortcuts

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