{"record":{"id":"27a0ba30ae780bb8","repo":"TheAlgorithms/Go","slug":"dequeue-is-empty-we-got-an-error","errorCode":null,"errorMessage":"dequeue is empty we got an error","messagePattern":"dequeue is empty we got an error","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"structure/queue/queuelinklistwithlist.go","lineNumber":39,"sourceCode":"\tqueue *list.List\n}\n\n// Enqueue will be added new value\nfunc (lq *LQueue) Enqueue(value any) {\n\tlq.queue.PushBack(value)\n}\n\n// Dequeue will be removed the first value that input (First In First Out - FIFO)\nfunc (lq *LQueue) Dequeue() error {\n\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}","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/structure/queue/queuelinklistwithlist.go#L21-L57","documentation":"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()).","triggerScenarios":"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().","commonSituations":"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.","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."],"exampleFix":"// before\nval, _ := q.Dequeue()\nuse(val)\n// after\nif q.Empty() {\n    return // queue drained\n}\nval, err := q.Dequeue()\nif err != nil {\n    return err\n}\nuse(val)","handlingStrategy":"validation","validationCode":"if q.Empty() {\n    return errors.New(\"cannot dequeue: queue is empty\")\n}\nval, err := q.Dequeue()","typeGuard":"func dequeueSafe(q *queue.LQueue) (any, bool) {\n    if q.Empty() {\n        return nil, false\n    }\n    v, err := q.Dequeue()\n    return v, err == nil\n}","tryCatchPattern":"val, err := q.Dequeue()\nif err != nil {\n    if strings.Contains(err.Error(), \"empty\") {\n        return // drained, stop consuming\n    }\n    return err\n}","preventionTips":["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."],"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"}