TheAlgorithms/Go · warning

queue is empty

Error message

queue is empty

What it means

Dequeue removes and returns the item at the front of the ring buffer. On an empty queue there is nothing to return, so it returns the zero value of T together with this error to signal that no item was dequeued.

Source

Thrown at structure/circularqueue/circularqueuearray.go:60

// 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 {
		cq.front = -1
		cq.rear = -1
	} else {
		cq.front = (cq.front + 1) % cq.size
	}
	return retVal, nil
}

// IsFull checks if the queue is full.
func (cq *CircularQueue[T]) IsFull() bool {
	return (cq.front == 0 && cq.rear == cq.size-1) || cq.front == cq.rear+1
}

// IsEmpty checks if the queue is empty.
func (cq *CircularQueue[T]) IsEmpty() bool {

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Check the returned error and treat it as an empty signal, not a failure
  2. Guard the call with cq.IsEmpty()
  3. Use a blocking primitive (channel or condition variable) if you need to wait for items

Example fix

// before
v, _ := cq.Dequeue()
process(v) // v is zero value on empty
// after
v, err := cq.Dequeue()
if err != nil {
    return err // queue was empty
}
process(v)
Defensive patterns

Strategy: try-catch

Validate before calling

if cq.IsEmpty() {
    return ErrNothingToDequeue
}

Try / catch

v, err := cq.Dequeue()
if err != nil {
    // empty queue: skip, wait, or return
    return nil
}
process(v)

Prevention

When it happens

Trigger: Calling Dequeue on a freshly created queue, or after all enqueued items have been removed (front and rear reset to -1), or in a race where multiple consumers drain the last item.

Common situations: Consumer loops polling faster than producers; forgetting to check IsEmpty before reading; racy consumer pools where the empty check and dequeue are not atomic.

Related errors


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