Skip to content

Repository files navigation

queue-go

A lightweight, generic, production-ready FIFO queue implementation for Go.

Built using a circular buffer for constant-time queue operations, automatic growth, and zero external dependencies.


Features

  • Generic (Queue[T])
  • FIFO semantics
  • Circular buffer implementation
  • Automatic capacity growth
  • Zero external dependencies
  • Memory-safe dequeue and clear operations
  • Fully tested
  • Benchmarked
  • Fuzz tested

Installation

go get github.com/raoamogh/queue-go

Requirements

  • Go 1.22+

Quick Start

package main

import (
	"fmt"

	"github.com/<username>/queue-go"
)

func main() {
	q := queue.New[int]()

	q.Enqueue(10)
	q.Enqueue(20)
	q.Enqueue(30)

	v, _ := q.Dequeue()
	fmt.Println(v)

	v, _ = q.Peek()
	fmt.Println(v)
}

Output

10
20

API

Constructors

New[T any]() *Queue[T]

NewWithCapacity[T any](capacity int) *Queue[T]

Operations

Enqueue(value T)

Dequeue() (T, error)

Peek() (T, error)

Clear()

Inspection

Len() int

Cap() int

Empty() bool

Error Handling

var ErrEmpty

Returned by:

  • Dequeue()
  • Peek()

when the queue is empty.


Complexity

Operation Complexity
Enqueue O(1) amortized
Dequeue O(1)
Peek O(1)
Len O(1)
Cap O(1)
Empty O(1)
Clear O(n)

Implementation

The queue is implemented using a dynamically growing circular buffer.

Unlike slice-based queues, dequeue operations do not shift elements.

Growth preserves FIFO ordering while minimizing allocations.

Removed elements are zeroed before being released to avoid retaining references and to assist Go's garbage collector.


Benchmarks

Run benchmarks locally:

go test -bench=. -benchmem

Example output:

BenchmarkQueue/Enqueue-16              xxxxx ns/op
BenchmarkQueue/Dequeue-16              xxxxx ns/op
BenchmarkQueue/Peek-16                 xxxxx ns/op
BenchmarkQueue/EnqueueDequeue-16       xxxxx ns/op
BenchmarkQueue/Growth-16               xxxxx ns/op
BenchmarkQueue/LargeFIFO-16            xxxxx ns/op
BenchmarkQueue/Clear-16                xxxxx ns/op

Benchmark numbers depend on hardware and Go version.


Testing

Run unit tests:

go test ./...

Run benchmarks:

go test -bench=. -benchmem

Run the race detector:

go test -race ./...

Run fuzz tests:

go test -fuzz=FuzzQueue

Design Goals

  • Small API surface
  • Predictable performance
  • Zero dependencies
  • Generic implementation
  • Production-ready code
  • Idiomatic Go
  • Easy to read and maintain

Non-Goals

This package intentionally does not provide:

  • Thread safety
  • Lock-free algorithms
  • Blocking queues
  • Priority queues
  • Double-ended queues (deque)
  • Persistent or disk-backed queues
  • Serialization
  • Concurrent producers/consumers

These concerns are better handled by dedicated packages.


Versioning

This project follows Semantic Versioning.

MAJOR.MINOR.PATCH
  • MAJOR for breaking API changes
  • MINOR for backward-compatible features
  • PATCH for bug fixes and performance improvements

Roadmap

v1.0.0

  • Generic queue
  • Circular buffer
  • Automatic growth
  • Unit tests
  • Benchmarks
  • Fuzz tests
  • GitHub Actions
  • Documentation

Future

  • Iterator support (if justified)
  • Optional capacity shrink API
  • Additional benchmark suites
  • Performance tuning based on profiling

New features will only be added if they preserve the library's simplicity and API stability.


Contributing

Contributions are welcome.

Before submitting a pull request:

  • Run all tests
  • Run benchmarks
  • Run the race detector
  • Keep the public API minimal
  • Maintain backwards compatibility where possible

License

MIT License.

About

A generic FIFO queue for Go using a dynamically growing circular buffer.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages