{"record":{"id":"d64558f0e60e4afe","repo":"TheAlgorithms/Go","slug":"size-must-be-greater-than-0","errorCode":null,"errorMessage":"size must be greater than 0","messagePattern":"size must be greater than 0","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"structure/circularqueue/circularqueuearray.go","lineNumber":31,"sourceCode":"\n// errors package: Provides functions to create and manipulate error values\nimport (\n\t\"errors\"\n)\n\n// CircularQueue represents a circular queue data structure.\ntype CircularQueue[T any] struct {\n\titems []T\n\tfront int\n\trear  int\n\tsize  int\n}\n\n// 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}","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/structure/circularqueue/circularqueuearray.go#L13-L49","documentation":"NewCircularQueue allocates a fixed-size ring buffer; it refuses to construct a queue with a non-positive capacity since a zero or negative capacity cannot store any items and would break the modulo arithmetic of the ring. The library returns this error instead of panicking on make([]T, size).","triggerScenarios":"Calling NewCircularQueue[T](0) or NewCircularQueue[T](n) with n < 0, typically when the size comes from an unvalidated config value, user input, or a computed expression that evaluated to zero.","commonSituations":"Config file with missing or zero capacity field; computing size from a slice length that was empty; default struct values in Go (int zero value) passed straight through without initialization.","solutions":["Ensure the size argument is a positive integer before calling NewCircularQueue","Add an explicit check or clamp: if size <= 0 { size = defaultSize }","Fix the source of the size value (config, env var, flag) to supply a valid positive number"],"exampleFix":"// before\nq, err := NewCircularQueue[int](cfg.QueueSize) // cfg.QueueSize == 0\n// after\nsize := cfg.QueueSize\nif size <= 0 {\n    size = 16 // sensible default\n}\nq, err := NewCircularQueue[int](size)","handlingStrategy":"validation","validationCode":"func validQueueSize(size int) bool { return size > 0 }\nif !validQueueSize(n) { return fmt.Errorf(\"invalid queue size %d\", n) }","typeGuard":null,"tryCatchPattern":"q, err := NewCircularQueue[T](n)\nif err != nil {\n    return fmt.Errorf(\"queue init: %w\", err)\n}","preventionTips":["Never pass raw config/flag values as size without checking > 0","Default to a positive constant when the size source is missing or zero","Centralize queue construction in one helper that clamps the size"],"tags":["go","validation","data-structures","circular-queue"],"backgroundTag":"invalid-capacity-argument","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}