TheAlgorithms/Go · error
error queue is empty
Error message
error queue is empty
What it means
LQueue.Front peeks at the front element without removing it. On an empty queue it returns this error (with an empty string placeholder). Note the error message says 'queue is empty' — it is a guard against peeking a container with no elements, thrown by callers such as ColorUsingBFS when they fail to check emptiness first.
Source
Thrown at structure/queue/queuelinklistwithlist.go:49
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
}
return "", fmt.Errorf("error queue is empty")
}
// Len it will return the length of list
func (lq *LQueue) Len() int {
return lq.queue.Len()
}
// Empty is check our list is empty or notView on GitHub (pinned to 5ba447ec5f)
Solutions
- Check q.Empty() (or q.Len() > 0) before calling Front().
- Inspect the returned error and branch on it instead of using the placeholder value.
- In BFS, verify the seed node is enqueued before the traversal loop starts.
Example fix
// before
val, _ := q.Front()
process(val.(int))
// after
if q.Empty() {
return errors.New("nothing to peek: queue is empty")
}
val, err := q.Front()
if err != nil {
return err
}
process(val.(int)) Defensive patterns
Strategy: validation
Validate before calling
if q.Empty() {
return errors.New("cannot peek: queue is empty")
}
val, err := q.Front() Type guard
func frontSafe(q *queue.LQueue) (any, bool) {
if q.Empty() {
return nil, false
}
v, err := q.Front()
return v, err == nil
} Try / catch
val, err := q.Front()
if err != nil {
if strings.Contains(err.Error(), "empty") {
return // nothing to peek
}
return err
} Prevention
- Guard Front() with Empty()/Len() checks.
- In BFS, enqueue the start node before the traversal loop that peeks.
- Handle the error explicitly; the empty-string return is a placeholder, not data.
When it happens
Trigger: Calling Front() on an empty or fully drained LQueue, e.g. BFS code that enqueues conditionally and then peeks a queue that never received a start node.
Common situations: Graph traversal (BFS) where the start vertex was never enqueued due to a logic bug, peeking after a Dequeue loop consumed everything, or checking Front before any Enqueue in producer/consumer code.
Related errors
- dequeue is empty we got an error
- stack list is empty
- failed to Encrypt
- failed to Decrypt
- no text to encrypt
AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02).
Data as JSON: /api/errors/6f15aafa19368169.
Report an issue: GitHub.