{"record":{"id":"6f15aafa19368169","repo":"TheAlgorithms/Go","slug":"error-queue-is-empty","errorCode":null,"errorMessage":"error queue is empty","messagePattern":"error queue is empty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"structure/queue/queuelinklistwithlist.go","lineNumber":49,"sourceCode":"\n\tif !lq.Empty() {\n\t\telement := lq.queue.Front()\n\t\tlq.queue.Remove(element)\n\n\t\treturn nil\n\t}\n\n\treturn fmt.Errorf(\"dequeue is empty we got an error\")\n}\n\n// Front it will return the front value\nfunc (lq *LQueue) Front() (any, error) {\n\tif !lq.Empty() {\n\t\tval := lq.queue.Front().Value\n\t\treturn val, nil\n\t}\n\n\treturn \"\", fmt.Errorf(\"error queue is empty\")\n}\n\n// Back it will return the back value\nfunc (lq *LQueue) Back() (any, error) {\n\tif !lq.Empty() {\n\t\tval := lq.queue.Back().Value\n\t\treturn val, nil\n\t}\n\n\treturn \"\", fmt.Errorf(\"error queue is empty\")\n}\n\n// Len it will return the length of list\nfunc (lq *LQueue) Len() int {\n\treturn lq.queue.Len()\n}\n\n// Empty is check our list is empty or not","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/structure/queue/queuelinklistwithlist.go#L31-L67","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nval, _ := q.Front()\nprocess(val.(int))\n// after\nif q.Empty() {\n    return errors.New(\"nothing to peek: queue is empty\")\n}\nval, err := q.Front()\nif err != nil {\n    return err\n}\nprocess(val.(int))","handlingStrategy":"validation","validationCode":"if q.Empty() {\n    return errors.New(\"cannot peek: queue is empty\")\n}\nval, err := q.Front()","typeGuard":"func frontSafe(q *queue.LQueue) (any, bool) {\n    if q.Empty() {\n        return nil, false\n    }\n    v, err := q.Front()\n    return v, err == nil\n}","tryCatchPattern":"val, err := q.Front()\nif err != nil {\n    if strings.Contains(err.Error(), \"empty\") {\n        return // nothing to peek\n    }\n    return err\n}","preventionTips":["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."],"tags":["go","queue","data-structure","empty-collection"],"backgroundTag":"empty-collection-access","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}