generic_sync

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: GPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package generic_sync 提供泛型同步原语

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lazy

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

Lazy 是一个惰性初始化的值

func NewLazy

func NewLazy[T any](init func() T) *Lazy[T]

NewLazy 创建一个惰性初始化的值

func (*Lazy[T]) Get

func (l *Lazy[T]) Get() T

Get 获取值(如果未初始化则初始化)

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Map 是一个泛型并发安全的 map

func NewMap

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

NewMap 创建一个新的泛型 Map

func (*Map[K, V]) Clear

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

Clear 清空 map

func (*Map[K, V]) Delete

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

Delete 删除键

func (*Map[K, V]) Keys

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

Keys 返回所有键

func (*Map[K, V]) Len

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

Len 返回元素数量

func (*Map[K, V]) Load

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

Load 获取键对应的值

func (*Map[K, V]) LoadAndDelete

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

LoadAndDelete 加载并删除值

func (*Map[K, V]) LoadOrStore

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

LoadOrStore 加载或存储值

func (*Map[K, V]) Range

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

Range 遍历所有键值对

func (*Map[K, V]) Store

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

Store 存储键值对

func (*Map[K, V]) Values

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

Values 返回所有值

type Once

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

Once 是一个泛型的 sync.Once,返回值

func (*Once[T]) Do

func (o *Once[T]) Do(f func() T) T

Do 执行函数并返回结果

func (*Once[T]) Value

func (o *Once[T]) Value() T

Value 获取已计算的值

type Pool

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

Pool 是一个泛型对象池

func NewPool

func NewPool[T any](newFunc func() T) *Pool[T]

NewPool 创建一个新的泛型 Pool

func (*Pool[T]) Get

func (p *Pool[T]) Get() T

Get 从池中获取对象

func (*Pool[T]) Put

func (p *Pool[T]) Put(x T)

Put 将对象放回池中

type RWMutexMap

type RWMutexMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

RWMutexMap 是带读写锁的泛型 map

func NewRWMutexMap

func NewRWMutexMap[K comparable, V any]() *RWMutexMap[K, V]

NewRWMutexMap 创建 RWMutexMap

func (*RWMutexMap[K, V]) Compute

func (m *RWMutexMap[K, V]) Compute(key K, f func(V, bool) V) V

Compute 计算并更新值

func (*RWMutexMap[K, V]) Delete

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

Delete 删除值

func (*RWMutexMap[K, V]) ForEach

func (m *RWMutexMap[K, V]) ForEach(f func(K, V))

ForEach 遍历所有键值对

func (*RWMutexMap[K, V]) Get

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

Get 获取值

func (*RWMutexMap[K, V]) GetOrSet

func (m *RWMutexMap[K, V]) GetOrSet(key K, value V) (V, bool)

GetOrSet 获取或设置值

func (*RWMutexMap[K, V]) Set

func (m *RWMutexMap[K, V]) Set(key K, value V)

Set 设置值

func (*RWMutexMap[K, V]) SetIfAbsent

func (m *RWMutexMap[K, V]) SetIfAbsent(key K, value V) bool

SetIfAbsent 如果不存在则设置

func (*RWMutexMap[K, V]) Size

func (m *RWMutexMap[K, V]) Size() int

Size 返回大小

type Value

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

Value 是一个泛型的原子值

func (*Value[T]) Load

func (v *Value[T]) Load() (T, bool)

Load 原子加载值

func (*Value[T]) Store

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

Store 原子存储值

func (*Value[T]) Swap

func (v *Value[T]) Swap(new T) (old T, loaded bool)

Swap 原子交换值

Jump to

Keyboard shortcuts

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