- Learn Data Structures and Algorithms with Golang
- Bhagvan Kommadi
- 159字
- 2021-06-24 15:37:50
The main method – queues
The main method creates two orders, and the priority of the orders is set to 2 and 1. In the following code, the queue will first process the order with the higher number on the priority value:
// main method
func main() {
var queue Queue
queue = make(Queue,0)
var order1 *Order = &Order{}
var priority1 int = 2
var quantity1 int = 20
var product1 string = "Computer"
var customerName1 string = "Greg White"
order1.New(priority1,quantity1,product1, customerName1)
var order2 *Order = &Order{}
var priority2 int = 1
var quantity2 int = 10
var product2 string = "Monitor"
var customerName2 string = "John Smith"
order2.New(priority2,quantity2,product2, customerName2)
queue.Add(order1)
queue.Add(order2)
var i int
for i=0; i< len(queue); i++ {
fmt.Println(queue[i])
}
}
Run the following commands to execute the queue.go file:
go run queue.go
After executing the preceding command, we get the following output:
![](https://epubservercos.yuewen.com/AA6936/19470380301498306/epubprivate/OEBPS/Images/af04ddad-2f0f-4ae8-9923-47f53957c4ff.png?sign=1739025879-MhGLfiiwJDUAaWfmgob9BZUCBirk9nJ2-0-fafa9e02cca705ef90b1e52bd8b8f640)
Let's take a look at Synchronized queue in the next section.