package
Version:
v1.32.0
Opens a new window with list of versions in this module.
Published: May 14, 2026
License: MIT
Opens a new window with license information.
Imports: 2
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
README
¶
Queue
This package provides a generic, thread-safe FIFO queue implementation for Go, supporting any type with automatic dynamic resizing.
Features
- Generic: Works with any type using Go generics (
Queue[T any])
- Thread-Safe: All operations are protected by mutex for concurrent access
- Dynamic Resizing: Automatic growth (2x) and shrinking (0.5x) based on usage
- Circular Buffer: Efficient O(1) enqueue/dequeue operations with minimal memory overhead
- Memory Efficient: Zero-value clearing prevents memory leaks
- FIFO Semantics: First-In-First-Out queue behavior
- Error Handling: Proper error types for empty queue operations
API
- NewQueue: Creates a new queue with specified initial capacity
- Enqueue: Adds an item to the end of the queue
- Dequeue: Removes and returns the front item (returns error if empty)
- Peek: Returns the front item without removing it (returns error if empty)
- Size: Returns the current number of elements in the queue
- Capacity: Returns the queue's current capacity
- IsEmpty: Returns true if the queue contains no elements
- Enqueue: ~28ns/op with minimal allocations
- Dequeue: ~11ns/op with minimal allocations
- Peek: ~5ns/op with zero allocations
- Memory: Minimal heap allocations during normal operations
Examples
For comprehensive examples of each function, please check out EXAMPLES.md
Documentation
¶
ErrEmptyQueue is returned when the queue is empty.
type Queue[T any] struct {
}
Queue is a generic, thread-safe FIFO queue implementation.
It uses a circular buffer internally and automatically grows or shrinks as needed.
Type Parameters:
T: The type of elements stored in the queue.
NewQueue creates a new Queue with the given capacity.
Capacity returns the queue's capacity.
Dequeue removes and returns the front item.
Returns ErrEmptyQueue if the queue is empty.
func (q *Queue[T]) Enqueue(item T)
Enqueue adds an item to the end of the queue.
IsEmpty returns true if the queue is empty.
Peek returns the front item without removing it.
Returns ErrEmptyQueue if the queue is empty.
Size returns the number of items in the queue.
Source Files
¶
Click to show internal directories.
Click to hide internal directories.