TheAlgorithms/Go · warning
ErrEmptyDequeue
ErrEmptyDequeue
Error message
DoublyEnded queue is empty, so can't perform this operation
What it means
ErrEmptyDequeue is the package-level sentinel error returned by every read/remove operation (DequeueFront, DequeueRear, Front, Rear) on a DoublyEndedQueue that holds no elements. It prevents index panics when accessing dq.deque[0] or the last element of an empty slice.
Source
Thrown at structure/deque/deque.go:15
// description: Double Ended Queue is a generalized version of Queue data structure that allows insert and delete at both ends.
// References:
// Wikipedia : https://en.wikipedia.org/wiki/Double-ended_queue
// Github: https://www.geeksforgeeks.org/deque-set-1-introduction-applications/
// author [Sayan](https://github.com/bose-sayan)
// Package deque implements a Double Ended Queue data structure.
package deque
import (
"errors"
)
// ErrEmptyDequeue is a custom error for handling cases when some dequeuing operation is performed on an empty deque.
var ErrEmptyDequeue = errors.New("DoublyEnded queue is empty, so can't perform this operation")
type DoublyEndedQueue[T any] struct {
deque []T
}
// New returns a new DoublyEndedQueue.
func New[T any]() *DoublyEndedQueue[T] {
return &DoublyEndedQueue[T]{deque: make([]T, 0)}
}
// EnqueueFront adds an item at the front of Deque.
func (dq *DoublyEndedQueue[T]) EnqueueFront(item T) {
dq.deque = append([]T{item}, dq.deque...)
}
// EnqueueRear adds an item at the rear of Deque.
func (dq *DoublyEndedQueue[T]) EnqueueRear(item T) {
dq.deque = append(dq.deque, item)View on GitHub (pinned to 5ba447ec5f)
Solutions
- Compare the returned error against ErrEmptyDequeue (errors.Is) and treat it as an empty signal
- Check the deque length or add an IsEmpty helper before popping
- Restructure to a blocking approach (channel) if consumers must wait for items
Example fix
// before
v, _ := dq.DequeueFront()
use(v) // zero value when empty
// after
v, err := dq.DequeueFront()
if errors.Is(err, deque.ErrEmptyDequeue) {
return // deque is empty
}
use(v) Defensive patterns
Strategy: type-guard
Validate before calling
if len(dq.Deque()) == 0 {
return deque.ErrEmptyDequeue
} Type guard
func isErrEmptyDequeue(err error) bool { return errors.Is(err, deque.ErrEmptyDequeue) } Try / catch
v, err := dq.DequeueRear()
if errors.Is(err, deque.ErrEmptyDequeue) {
return // deque drained
}
if err != nil {
return err
} Prevention
- Use errors.Is against the exported sentinel rather than string comparison
- Track deque size yourself if you pop in a loop
- Add an IsEmpty() wrapper in your codebase for all deques
When it happens
Trigger: Calling Front, Rear, DequeueFront, or DequeueRear when len(dq.deque) == 0 — e.g. on a queue created with New() but never populated, or after draining all elements.
Common situations: Consumer loops draining a deque until empty; popping both ends of a work-stealing deque without a size check; test code that forgot to seed the deque.
Related errors
AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02).
Data as JSON: /api/errors/e2426d517af93da7.
Report an issue: GitHub.