{"record":{"id":"1b2e71494cc3d92c","repo":"TheAlgorithms/Go","slug":"queue-is-full","errorCode":null,"errorMessage":"queue is full","messagePattern":"queue is full","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"structure/circularqueue/circularqueuearray.go","lineNumber":45,"sourceCode":"// NewCircularQueue creates a new CircularQueue with the given size.\n// Returns an error if the size is less than or equal to 0.\nfunc NewCircularQueue[T any](size int) (*CircularQueue[T], error) {\n\tif size <= 0 {\n\t\treturn nil, errors.New(\"size must be greater than 0\")\n\t}\n\treturn &CircularQueue[T]{\n\t\titems: make([]T, size),\n\t\tfront: -1,\n\t\trear:  -1,\n\t\tsize:  size,\n\t}, nil\n}\n\n// Enqueue adds an item to the rear of the queue.\n// 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 {","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/structure/circularqueue/circularqueuearray.go#L27-L63","documentation":"Enqueue appends an item at the rear of the fixed-capacity ring buffer. Because the queue is backed by a slice of fixed size, inserting into a full queue would overwrite live data, so the method returns this sentinel error instead.","triggerScenarios":"Calling Enqueue when the number of items already equals the capacity returned by Size(), e.g. enqueueing N+1 items into a queue created with size N, or repeatedly enqueueing without Dequeue.","commonSituations":"Producer goroutines outpacing consumers; bounded work queues where the consumer stalled; miscalculated buffer size for the workload.","solutions":["Check cq.IsFull() before Enqueue, or handle the returned error by waiting/draining","Call Dequeue to free a slot before retrying the Enqueue","Create the queue with a larger capacity via NewCircularQueue","Add backpressure (block or drop) in the producer when full"],"exampleFix":"// before\nif err := cq.Enqueue(item); err != nil { /* ignored overwrite risk */ }\n// after\nif cq.IsFull() {\n    _, _ = cq.Dequeue() // make room\n}\nif err := cq.Enqueue(item); err != nil {\n    return err\n}","handlingStrategy":"validation","validationCode":"if cq.IsFull() {\n    return errors.New(\"cannot enqueue: circular queue is full\")\n}","typeGuard":null,"tryCatchPattern":"if err := cq.Enqueue(item); err != nil {\n    if err.Error() == \"queue is full\" {\n        _, _ = cq.Dequeue() // drop oldest, retry\n        return cq.Enqueue(item)\n    }\n    return err\n}","preventionTips":["Check IsFull() before Enqueue in producer loops","Size the queue for peak load with headroom","Add backpressure or drop-oldest policy for bounded producers"],"tags":["go","data-structures","circular-queue","overflow"],"backgroundTag":"queue-full","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}