emirpasic/gods · error

Invalid maxSize, should be at least 1

Error message

Invalid maxSize, should be at least 1

What it means

A programming-time guard panic raised by circularbuffer.New when the maxSize argument is less than 1. A circular buffer is a fixed-capacity structure: the backing slice and index wrap-around arithmetic (end/start modulo maxSize) are meaningless for a zero or negative capacity, and zero would make the buffer permanently full/broken, so the constructor refuses such arguments instead of returning a broken queue.

Source

Thrown at queues/circularbuffer/circularbuffer.go:38

// Assert Queue implementation
var _ queues.Queue[int] = (*Queue[int])(nil)

// Queue holds values in a slice.
type Queue[T comparable] struct {
	values  []T
	start   int
	end     int
	full    bool
	maxSize int
	size    int
}

// New instantiates a new empty queue with the specified size of maximum number of elements that it can hold.
// This max size of the buffer cannot be changed.
func New[T comparable](maxSize int) *Queue[T] {
	if maxSize < 1 {
		panic("Invalid maxSize, should be at least 1")
	}
	queue := &Queue[T]{maxSize: maxSize}
	queue.Clear()
	return queue
}

// Enqueue adds a value to the end of the queue
func (queue *Queue[T]) Enqueue(value T) {
	if queue.Full() {
		queue.Dequeue()
	}
	queue.values[queue.end] = value
	queue.end = queue.end + 1
	if queue.end >= queue.maxSize {
		queue.end = 0
	}
	if queue.end == queue.start {
		queue.full = true

View on GitHub (pinned to 1d83d5ae39)

Solutions

  1. Pass a maxSize of at least 1 when calling circularbuffer.New (e.g. New[int](16) for a 16-element ring buffer).
  2. Validate the capacity before constructing: if maxSize < 1 { return fmt.Errorf(...) } or clamp it, e.g. max(maxSize, 1), at the call site.
  3. Compute the capacity rather than hardcoding it and check it is positive; trace where a zero/negative value originates (config, environment variable, length of an empty slice).
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at queues/circularbuffer/circularbuffer.go:38 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of emirpasic/gods@1d83d5ae39 (2026-09-03). Data as JSON: /api/errors/e992d050d8c65434. Report an issue: GitHub.