{"record":{"id":"cf55aefcdfc48289","repo":"TheAlgorithms/Go","slug":"queue-is-empty","errorCode":null,"errorMessage":"queue is empty","messagePattern":"queue is empty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"structure/circularqueue/circularqueuearray.go","lineNumber":60,"sourceCode":"// Returns an error if the queue is full.\nfunc (cq *CircularQueue[T]) Enqueue(item T) error {\n\tif cq.IsFull() {\n\t\treturn errors.New(\"queue is full\")\n\t}\n\tif cq.IsEmpty() {\n\t\tcq.front = 0\n\t}\n\tcq.rear = (cq.rear + 1) % cq.size\n\tcq.items[cq.rear] = item\n\treturn nil\n}\n\n// Dequeue removes and returns the item from the front of the queue.\n// Returns an error if the queue is empty.\nfunc (cq *CircularQueue[T]) Dequeue() (T, error) {\n\tif cq.IsEmpty() {\n\t\tvar zeroValue T\n\t\treturn zeroValue, errors.New(\"queue is empty\")\n\t}\n\tretVal := cq.items[cq.front]\n\tif cq.front == cq.rear {\n\t\tcq.front = -1\n\t\tcq.rear = -1\n\t} else {\n\t\tcq.front = (cq.front + 1) % cq.size\n\t}\n\treturn retVal, nil\n}\n\n// IsFull checks if the queue is full.\nfunc (cq *CircularQueue[T]) IsFull() bool {\n\treturn (cq.front == 0 && cq.rear == cq.size-1) || cq.front == cq.rear+1\n}\n\n// IsEmpty checks if the queue is empty.\nfunc (cq *CircularQueue[T]) IsEmpty() bool {","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/structure/circularqueue/circularqueuearray.go#L42-L78","documentation":"Dequeue removes and returns the item at the front of the ring buffer. On an empty queue there is nothing to return, so it returns the zero value of T together with this error to signal that no item was dequeued.","triggerScenarios":"Calling Dequeue on a freshly created queue, or after all enqueued items have been removed (front and rear reset to -1), or in a race where multiple consumers drain the last item.","commonSituations":"Consumer loops polling faster than producers; forgetting to check IsEmpty before reading; racy consumer pools where the empty check and dequeue are not atomic.","solutions":["Check the returned error and treat it as an empty signal, not a failure","Guard the call with cq.IsEmpty()","Use a blocking primitive (channel or condition variable) if you need to wait for items"],"exampleFix":"// before\nv, _ := cq.Dequeue()\nprocess(v) // v is zero value on empty\n// after\nv, err := cq.Dequeue()\nif err != nil {\n    return err // queue was empty\n}\nprocess(v)","handlingStrategy":"try-catch","validationCode":"if cq.IsEmpty() {\n    return ErrNothingToDequeue\n}","typeGuard":null,"tryCatchPattern":"v, err := cq.Dequeue()\nif err != nil {\n    // empty queue: skip, wait, or return\n    return nil\n}\nprocess(v)","preventionTips":["Always check the (T, error) pair — never discard err","Guard hot loops with IsEmpty()","Use channels or sync primitives when consumers must block"],"tags":["go","data-structures","circular-queue","underflow"],"backgroundTag":"queue-empty","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}