TheAlgorithms/Go · error
dequeue is empty we got an error
Error message
dequeue is empty we got an error
What it means
LQueue.Dequeue removes and returns the front element of the linked-list-backed queue. When the queue is empty there is nothing to remove, so the method returns this error instead of a value; the returned value is nil in that case. It signals caller misuse (dequeueing without checking Empty()/Len()).
Source
Thrown at structure/queue/queuelinklistwithlist.go:39
queue *list.List
}
// Enqueue will be added new value
func (lq *LQueue) Enqueue(value any) {
lq.queue.PushBack(value)
}
// Dequeue will be removed the first value that input (First In First Out - FIFO)
func (lq *LQueue) Dequeue() error {
if !lq.Empty() {
element := lq.queue.Front()
lq.queue.Remove(element)
return nil
}
return fmt.Errorf("dequeue is empty we got an error")
}
// Front it will return the front value
func (lq *LQueue) Front() (any, error) {
if !lq.Empty() {
val := lq.queue.Front().Value
return val, nil
}
return "", fmt.Errorf("error queue is empty")
}
// Back it will return the back value
func (lq *LQueue) Back() (any, error) {
if !lq.Empty() {
val := lq.queue.Back().Value
return val, nil
}View on GitHub (pinned to 5ba447ec5f)
Solutions
- Check q.Empty() or q.Len() > 0 before each Dequeue call.
- Handle the returned error: on this error, treat the queue as drained and stop dequeuing.
- Use the two-return form `val, err := q.Dequeue()` and never ignore err; a nil val with non-nil err is not a real element.
Example fix
// before
val, _ := q.Dequeue()
use(val)
// after
if q.Empty() {
return // queue drained
}
val, err := q.Dequeue()
if err != nil {
return err
}
use(val) Defensive patterns
Strategy: validation
Validate before calling
if q.Empty() {
return errors.New("cannot dequeue: queue is empty")
}
val, err := q.Dequeue() Type guard
func dequeueSafe(q *queue.LQueue) (any, bool) {
if q.Empty() {
return nil, false
}
v, err := q.Dequeue()
return v, err == nil
} Try / catch
val, err := q.Dequeue()
if err != nil {
if strings.Contains(err.Error(), "empty") {
return // drained, stop consuming
}
return err
} Prevention
- Check Empty()/Len() before every Dequeue.
- Never discard the error return of Dequeue.
- In drain loops, terminate on the empty error or an explicit length check.
When it happens
Trigger: Calling Dequeue() on a freshly created LQueue, or more times than the number of Enqueue() calls, e.g. looping `for { q.Dequeue() }` until it breaks without checking Empty().
Common situations: Consumer loops draining a queue faster than producers fill it, miscounted batch sizes (dequeuing n+1 items after enqueueing n), or shared queues consumed by multiple goroutines without synchronization.
Related errors
AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02).
Data as JSON: /api/errors/27a0ba30ae780bb8.
Report an issue: GitHub.