A simple, generic implementation of Priority Queue, based on container/heap.
This package is built on top of the functionality already provided by
container/heap, and also adds various convenience methods for
creating new priority queues, predicates for testing whether the queue
is empty, synchronization so it can be safely used by multiple
goroutines.
Execute the following command.
go get -v gopkg.in/dnaeon/go-priorityqueue.v1
package main import ( "fmt" pq "gopkg.in/dnaeon/go-priorityqueue.v1" ) func main() { // Create a new priority queue queue := pq.New[string, int64](pq.MinHeap) // Insert items in the queue queue.Put("apple", 10) queue.Put("banana", 3) queue.Put("pear", 20) queue.Put("orange", 15) // Update priority of an item queue.Update("banana", 42) for !queue.IsEmpty() { item := queue.Get() fmt.Printf("%s: %d\n", item.Value, item.Priority) } // Output: // apple: 10 // orange: 15 // pear: 20 // banana: 42 }
Make sure to check the included test cases for additional examples.
Run the tests.
make testgo-priorityqueue is Open Source and licensed under the BSD
License.
Some parts of go-priorityqueue re-use code from the examples in
container/heap.