Categorygithub.com/kordax/basic-utils/queue
package
1.1.1
Repository: https://github.com/kordax/basic-utils.git
Documentation: pkg.go.dev

# README

Queues Library

This library provides a rich set of data structures for queuing in Go. It includes implementations for both a FIFO ( First-In-First-Out) queue and a priority queue.

Features

  • Generic Implementation: Both the FIFO and priority queues are implemented using Go's new generics feature, allowing you to queue items of any type.
  • Thread-Safety: Built with concurrency in mind, you can safely use the queues across multiple goroutines.
  • Timeouts for Polling: The Poll function allows consumers to wait for items with a timeout.

Structures

FIFOQueueImpl

A standard FIFO (First-In-First-Out) queue implementation.

Methods

  • NewFIFOQueue: Initializes a new FIFO queue with optional initial elements.
  • Queue: Enqueues an item to the back of the queue.
  • Fetch: Dequeues an item from the front of the queue without waiting.
  • Poll: Dequeues an item from the front of the queue or waits for a specified timeout.

PriorityQueueImpl

A priority queue where items can be enqueued with a priority level, and dequeuing will retrieve the highest priority item.

Methods

  • NewPrioritizedPriorityQueue: Initializes a new empty priority queue.
  • Queue: Enqueues an item with a specified priority.
  • Fetch: Dequeues the highest-priority item from the queue without waiting.
  • Poll: Dequeues the highest-priority item from the queue or waits for a specified timeout.

Dependencies

The library uses the container/heap package for the priority queue implementation and an opt package for optional value handling.

Usage

First, import the queue package in your Go code:

package myprogram

import "github.com/kordax/basic-utils/queue"

// To use the FIFO queue:
q := queue.NewFIFOQueue[int](1, 2, 3)
q.Queue(4)
item := q.Poll(5 * time.Second)

For the priority queue:

pq := queue.NewPrioritizedPriorityQueueint
pq.Queue(1, 3) // The number 3 here is the priority
pq.Queue(2, 1)
item := pq.Poll(5 * time.Second)

Author

Developed by @kordax (Dmitry Morozov)

Valoru Software