Skip to content
This repository has been archived by the owner. It is now read-only.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.travis.yml
LICENSE
Makefile
README.md
queue.go
queue_test.go

README.md

A GoRoutine safe queue for Golang Build Status GoDoc

It is similar to the Queue of Python.

Installation

go get github.com/damnever/goqueue

Example

Just for example, I use Queue.Get(0) and Queue.PutNoWait(value) more and often, but channel is not a right way do that...

package main

import (
    "fmt"
    "sync"

    "github.com/Damnever/goqueue"
)

func main() {
    queue := goqueue.New(0)
    wg := &sync.WaitGroup{}

    worker := func(queue *goqueue.Queue) {
        defer wg.Done()
        for !queue.IsEmpty() {
            val, err := queue.Get(0)
            if err != nil {
                fmt.Println("Unexpect Error: %v\n", err)
            }
            num := val.(int)
            fmt.Printf("-> %v\n", num)
            if num%3 == 0 {
                for i := num + 1; i < num+3; i++ {
                    queue.PutNoWait(i)
                }
            }
        }
    }

    go func() {
        defer wg.Done()
        for i := 0; i <= 27; i += 3 {
            queue.PutNoWait(i)
        }
    }()

    for i := 0; i < 5; i++ {
        go worker(queue)
    }

    wg.Add(6)
    wg.Wait()

    fmt.Println("All task done!!!")
}
You can’t perform that action at this time.