concurrency

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConcurrentRun

func ConcurrentRun[T any](funcs []func() (T, error)) <-chan TaskResult[T]

ConcurrentRun 并发执行传入的函数集合,返回结果通道和错误通道。 每个函数的结果会发送到结果通道,错误(包括 panic 转换的错误)发送到错误通道。 当所有函数执行完成后,通道会自动关闭。

Types

type ConcurrentFunc

type ConcurrentFunc[T any] interface {
	ToConcurrentFunc() func() (T, error)
}

ConcurrentFunc 定义一个接口

type GenericSyncMap

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

GenericSyncMap 是一个使用 sync.Map 封装的泛型 Map

func NewGenericSyncMap

func NewGenericSyncMap[K comparable, V any]() *GenericSyncMap[K, V]

func NewGenericSyncMapFromMap

func NewGenericSyncMapFromMap[K comparable, V any](m map[K]V) *GenericSyncMap[K, V]

func (*GenericSyncMap[K, V]) Delete

func (gm *GenericSyncMap[K, V]) Delete(key K)

Delete 方法从 Map 中删除键值对

func (*GenericSyncMap[K, V]) Get

func (gm *GenericSyncMap[K, V]) Get(key K) (V, bool)

Get 方法从 Map 中检索值

func (*GenericSyncMap[K, V]) GetOr

func (gm *GenericSyncMap[K, V]) GetOr(k K, dV V) (v V)

func (*GenericSyncMap[K, V]) GetOrElse

func (gm *GenericSyncMap[K, V]) GetOrElse(k K, fn func() V) (v V)

func (*GenericSyncMap[K, V]) Keys

func (gm *GenericSyncMap[K, V]) Keys() []K

Keys 方法返回 Map 中的所有键

func (*GenericSyncMap[K, V]) Range

func (gm *GenericSyncMap[K, V]) Range(f func(key K, value V) bool)

Range 方法遍历 Map 中的所有键值对

func (*GenericSyncMap[K, V]) Set

func (gm *GenericSyncMap[K, V]) Set(key K, value V)

Set 方法将键值对添加到 Map 中

func (*GenericSyncMap[K, V]) ToMap

func (gm *GenericSyncMap[K, V]) ToMap() map[K]V

ToMap 方法将 GenericSyncMap 转换为普通 map

func (*GenericSyncMap[K, V]) Values

func (gm *GenericSyncMap[K, V]) Values() []V

Values 方法返回 Map 中的所有值

type SafeMap

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

SafeMap 使用分片锁的线程安全map

func NewSafeMap

func NewSafeMap[K comparable, V any](shardCount int) *SafeMap[K, V]

NewSafeMap 创建一个带有指定分片数量的线程安全map

func NewSafeMapFromMap

func NewSafeMapFromMap[K comparable, V any](m map[K]V, shardCount int) *SafeMap[K, V]

NewSafeMapFromMap 将普通map转换为ConcurrentMap

func (*SafeMap[K, V]) Clear

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

Clear 清空map

func (*SafeMap[K, V]) Delete

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

Delete 删除键对应的值

func (*SafeMap[K, V]) ForEach

func (m *SafeMap[K, V]) ForEach(fn func(K, V) bool)

ForEach 遍历map中的所有键值对

func (*SafeMap[K, V]) Get

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

Get 获取键对应的值,不存在时返回nil

func (*SafeMap[K, V]) GetOr

func (m *SafeMap[K, V]) GetOr(key K, dV V) V

GetOr 获取键对应的值,不存在时返回给定的默认值

func (*SafeMap[K, V]) GetOrElse

func (m *SafeMap[K, V]) GetOrElse(key K, fn func() V) V

GetOrElse 获取键对应的值,不存在时调用给定的函数并返回其结果

func (*SafeMap[K, V]) Keys

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

Keys 返回map中的所有键

func (*SafeMap[K, V]) Length

func (m *SafeMap[K, V]) Length() int

Length 返回map中的元素数量

func (*SafeMap[K, V]) Set

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

Set 设置键值对

func (*SafeMap[K, V]) ToMap

func (m *SafeMap[K, V]) ToMap() map[K]V

ToMap 将 SafeMap 转换为一个简单的 map

func (*SafeMap[K, V]) Values

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

Values 返回map中的所有值

type TaskResult

type TaskResult[T any] struct {
	Index int   // 函数索引
	Value T     // 成功时的返回值
	Err   error // 失败时的错误(包括 panic)
}

Jump to

Keyboard shortcuts

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