Documentation
¶
Overview ¶
Package collectionx 提供泛型并发/集合容器微件:Set、环形缓冲 Ring、 阻塞队列 Queue、并发安全 SafeMap。全部零依赖(仅标准库)。
设计参考 go-zero collection 的容器件(Set/Ring/fifo.Queue/SafeMap), 全面泛型化并按 Go 习惯收敛语义:
- Set / Ring 为非并发安全(单 goroutine 或外部加锁使用);
- Queue / SafeMap 内建并发安全;
- Ring 溢出覆盖最旧值(有界有损),Queue 无界无损。
用法:
s := collectionx.NewSet(1, 2, 3) s.Add(4) q := collectionx.NewQueue[int]() q.Put(1) v, ok := q.Take() // 阻塞直至有元素或关闭
Index ¶
- type Queue
- type Ring
- type SafeMap
- type Set
- func (s Set[T]) Add(v T)
- func (s Set[T]) AddAll(items ...T)
- func (s Set[T]) Clone() Set[T]
- func (s Set[T]) Contains(v T) bool
- func (s Set[T]) Difference(other Set[T]) Set[T]
- func (s Set[T]) Intersect(other Set[T]) Set[T]
- func (s Set[T]) Items() []T
- func (s Set[T]) Len() int
- func (s Set[T]) Range(fn func(v T) bool)
- func (s Set[T]) Remove(v T)
- func (s Set[T]) Union(other Set[T]) Set[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Queue ¶
type Queue[T any] struct { // contains filtered or unexported fields }
Queue 无界阻塞 FIFO 队列(并发安全):多生产者/多消费者, Take 阻塞直至有元素或队列关闭。Close 后 Put 直接 panic(与 向已关闭 channel 发送的语义一致)。
func (*Queue[T]) Close ¶
func (q *Queue[T]) Close()
Close 关闭队列:唤醒全部阻塞的 Take(取完余量后返回 false); 幂等。关闭后 Put 会 panic。
type Ring ¶
type Ring[T any] struct { // contains filtered or unexported fields }
Ring 定长环形缓冲(非并发安全):生产快于消费最多保留最新 n 个元素, 超出后 Add 覆盖最旧值且保序 FIFO(有界有损,适合"只要最新 n 条"的场景, 如最近请求采样、滑动日志)。
type SafeMap ¶
type SafeMap[K comparable, V any] struct { // contains filtered or unexported fields }
SafeMap 并发安全的泛型映射(RWMutex 保护),对标 go-zero collection/SafeMap 的泛型化替代。零值不可直接使用,须经 NewSafeMap 创建。
type Set ¶
type Set[T comparable] map[T]struct{}
Set 泛型集合(非并发安全:单 goroutine 使用,或由调用方加锁保护)。
func (Set[T]) Difference ¶
Difference 差集:返回存在于 s 但不存在于 other 的新集合。
Click to show internal directories.
Click to hide internal directories.