paint-brush
Today I Learned: Using Priority Queue in Golangby@imantumorang
15,745 reads
15,745 reads

Today I Learned: Using Priority Queue in Golang

by Iman TumorangFebruary 5th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Priority Queue, is one of way how we manage data flow. In term of programming, we know 2 of ways to control data flow.

Company Mentioned

Mention Thumbnail

Coin Mentioned

Mention Thumbnail
featured image - Today I Learned: Using Priority Queue in Golang
Iman Tumorang HackerNoon profile picture

Is very easy and we can just use it without thinking about how to make/create it.

Priority Queue, is one of way how we manage data flow. In term of programming, we know 2 of ways to control data flow.


  • LIFO: Last In First Out A method of organizing data or manipulating data where the last or the newest data will processed first. In programming and data structure we also know this as Stack.



  • FIFO: First In First OutA method of organizing data where the first data or the oldest data will the first processed. In programming and data structure, we call this queue. Example in real life: Is when you ordering food in restaurant, the first person who ordered will served first.

Priority Queue is one of kind queue, but has priority or weighted. I won’t tell about this in details, you can learn about it from the Internet.

So how to implement this in Golang?

So moving from my real case, in my current project, I must use the priority queue programming pattern. For example, let’s say this project is about making inventory management of goods that easily expired.

Let’s say we have a warehouse, contains: Carrot that will expired in 30 days, Potato for 45 days, Rice for 100 Days, and Spinach only can used in 5 days. So to deliver this items, we must take care their expiry time, and to avoid the expiration, obviously we will delivered the goods from the lowest to the highest expiry time.

So the order will:

  • Spinach
  • Carrot
  • Potato
  • Rice

Example How Priority Queue In Real Case

Once again, how to implement this to Golang? As we know, for some people, including me, sometimes need a lot of time of thinking, how to design the Linkedlist and queue programmatically. Because it’s maybe hard, it need more understanding how to structuring data programmatically.

Implementing Priority Queue in Golang

Luckily, Golang has provide a package to help us to implement priority queue to real project cases, whatever it case. We just implement the provided interface with our own struct.

This package called: container/heap, this package already have the builtin logic, we just need to implement the interface function to our struct.

For example, for the above case, in inventory management. We can make the priority queue based on the expiry here.

Thanks to Golang maintainer, because it just a little bit of work to create priority queue. So we don’t need to overthinking again, how to create priority queue manually.

Well, that’s all. For some people maybe already recognized this package, but for my case, I just knew it. And this is really helpful. And for sure, today I learned something new.

If you found this article useful, or interesting to read, please kindly share to your network circle, or you can just give claps to help other to reach this. If you have any question or something, just put a response below.