TheAlgorithms/Go · error

queue is full

Error message

queue is full

What it means

Enqueue appends an item at the rear of the fixed-capacity ring buffer. Because the queue is backed by a slice of fixed size, inserting into a full queue would overwrite live data, so the method returns this sentinel error instead.

Source

Thrown at structure/circularqueue/circularqueuearray.go:45

// NewCircularQueue creates a new CircularQueue with the given size.
// Returns an error if the size is less than or equal to 0.
func NewCircularQueue[T any](size int) (*CircularQueue[T], error) {
	if size <= 0 {
		return nil, errors.New("size must be greater than 0")
	}
	return &CircularQueue[T]{
		items: make([]T, size),
		front: -1,
		rear:  -1,
		size:  size,
	}, nil
}

// Enqueue adds an item to the rear of the queue.
// Returns an error if the queue is full.
func (cq *CircularQueue[T]) Enqueue(item T) error {
	if cq.IsFull() {
		return errors.New("queue is full")
	}
	if cq.IsEmpty() {
		cq.front = 0
	}
	cq.rear = (cq.rear + 1) % cq.size
	cq.items[cq.rear] = item
	return nil
}

// Dequeue removes and returns the item from the front of the queue.
// Returns an error if the queue is empty.
func (cq *CircularQueue[T]) Dequeue() (T, error) {
	if cq.IsEmpty() {
		var zeroValue T
		return zeroValue, errors.New("queue is empty")
	}
	retVal := cq.items[cq.front]
	if cq.front == cq.rear {

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Check cq.IsFull() before Enqueue, or handle the returned error by waiting/draining
  2. Call Dequeue to free a slot before retrying the Enqueue
  3. Create the queue with a larger capacity via NewCircularQueue
  4. Add backpressure (block or drop) in the producer when full

Example fix

// before
if err := cq.Enqueue(item); err != nil { /* ignored overwrite risk */ }
// after
if cq.IsFull() {
    _, _ = cq.Dequeue() // make room
}
if err := cq.Enqueue(item); err != nil {
    return err
}
Defensive patterns

Strategy: validation

Validate before calling

if cq.IsFull() {
    return errors.New("cannot enqueue: circular queue is full")
}

Try / catch

if err := cq.Enqueue(item); err != nil {
    if err.Error() == "queue is full" {
        _, _ = cq.Dequeue() // drop oldest, retry
        return cq.Enqueue(item)
    }
    return err
}

Prevention

When it happens

Trigger: Calling Enqueue when the number of items already equals the capacity returned by Size(), e.g. enqueueing N+1 items into a queue created with size N, or repeatedly enqueueing without Dequeue.

Common situations: Producer goroutines outpacing consumers; bounded work queues where the consumer stalled; miscalculated buffer size for the workload.

Related errors


AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02). Data as JSON: /api/errors/1b2e71494cc3d92c. Report an issue: GitHub.