csync

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package csync provides generic thread-safe containers.

Index

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 generic thread-safe map. All operations are guarded by a RWMutex.

func NewMap

func NewMap[K comparable, V any]() *Map[K, V]

NewMap creates an empty Map.

func NewMapFrom

func NewMapFrom[K comparable, V any](src map[K]V) *Map[K, V]

NewMapFrom creates a Map pre-populated with entries from src.

func (*Map[K, V]) Copy

func (m *Map[K, V]) Copy() map[K]V

Copy returns a shallow clone of the underlying map.

func (*Map[K, V]) Del

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

Del removes a key.

func (*Map[K, V]) Get

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

Get returns the value for key and whether it was found.

func (*Map[K, V]) GetOrSet

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

GetOrSet returns the existing value for key if present. Otherwise it calls init(), stores and returns the result.

func (*Map[K, V]) Len

func (m *Map[K, V]) Len() int

Len returns the number of entries.

func (*Map[K, V]) Range

func (m *Map[K, V]) Range(fn func(K, V) bool)

Range calls fn for each entry. If fn returns false, iteration stops. Operates on a snapshot for safety.

func (*Map[K, V]) Reset

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

Reset clears all entries.

func (*Map[K, V]) Set

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

Set stores a key-value pair.

type Value

type Value[T any] struct {
	// contains filtered or unexported fields
}

Value is a generic thread-safe scalar wrapper. Use it for values that can be read and written concurrently by multiple goroutines.

For pointer, slice, or map types, use Map or Slice instead — those types have reference semantics that a simple mutex guard cannot make safe.

func NewValue

func NewValue[T any](initial T) *Value[T]

NewValue creates a Value with the given initial value.

func (*Value[T]) Get

func (v *Value[T]) Get() T

Get returns the current value under a read lock.

func (*Value[T]) Set

func (v *Value[T]) Set(val T)

Set updates the value under a write lock.

func (*Value[T]) Update

func (v *Value[T]) Update(fn func(T) T) T

Update atomically applies fn to the current value and stores the result.

Jump to

Keyboard shortcuts

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