goqueue

package module
v0.0.0-...-f1771bf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2014 License: GPL-3.0 Imports: 2 Imported by: 3 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

GoQueue

Build Status

GoQueue is a simple thread safe empty interface Queue implementation in Golang

Installation

To install GoQueue just use go get in your preferred interpreter

go get github.com/DamnWidget/goqueue

Usage

GoQueue is pretty straightforward to use, you just have to create a new Queue and push or pop whatever element you want from or to it.

Unlimited Queues

Queues can have a maximum capacity size or be unlimited, to create an unlimited queue just call the New method with no parameters:

// create a new unlimited Queue
q := goqueue.New()
Sized Queues

If you want to create a sized Queue, then pass the desired size of the queue as first and only one parameter to the New function (if more than one parameter is passed, only the first one is taken into account)

// create a new Queue with a maximum size of 1000 elements
q := goqueue.New(1000)
Queue size operations

The Queue size and capacity properties are exported via the Len and Cap methods respectively.

q := goqueue.New(1000)
fmt.Println(q.Len())
fmt.Println(q.Cap())

Will output:

0
1000
Push elements to the end of the Queue

To push an element to the end of the Queue we just call the method Push using the element as unique argument.

q := goqueue.New()
err := q.Push("Test")

Remember that any type of element can be pushed to the GoQueue, including your own defined types.

type Customer struct {
    name        string
    customer_id int
}
err := q.Push(&Customer{"Test Customer", 1})

If we try to push an element beyond the size limit of the Queue the Push method returns an error so you should always check the returned value. If the Push is successful then, it returns nil.

Pop elements from the end of the Queue

Elements are extracted from the end of the Queue (as this Queue is a FIFO data structure).

customer = q.Pop()

If the Queue is empty, Pop returns nil so you will always check the returned value and use in your logic

q := goqueue.Queue()
// push things here
...

for {
    item := q.Pop()
    if item == nil {
        break
    }
    // do something useful here
}
Return all Queue elements at once

We can get all the elements from a Queue without extract them (and without mutate the Queue) if we need to do so using the Values method.

for item := range q.Values() {
    // do something clever here with item
}

Thread Safe Operations

All the operations in the Queue are thread safe so we can share a Queue instance safely between any number of goroutines as we need.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

Thread safe FIFO Queue data structure

func New

func New(size ...int64) *Queue

Create a new queue If the parameter size is passed, the queue will be of that fixed size

func (*Queue) Cap

func (queue *Queue) Cap() int64

Return the Queue max capacity (size) NOTE: -1 is returned for unlimited Queues

func (*Queue) Len

func (queue *Queue) Len() int64

Return the Queue current length

func (*Queue) Pop

func (queue *Queue) Pop() interface{}

Extract an element from the end of the queue

func (*Queue) Push

func (queue *Queue) Push(element interface{}) error

Push an element at the end of the Queue

func (*Queue) Values

func (queue *Queue) Values() []interface{}

Get all the values from the Queue as an interface{} slice

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL