The Add method

In the following code snippet, the Add method on the Queue class takes the order parameter and adds it to Queue based on the priority. Based on this, the location of the order parameter is found by comparing it with the priority parameter:

//Add method adds the order to the queue
func (queue *Queue) Add(order *Order){
if len(*queue) == 0 {
*queue = append(*queue,order)
} else{
var appended bool
appended = false
var i int
var addedOrder *Order
for i, addedOrder = range *queue {
if order.priority > addedOrder.priority {
*queue = append((*queue)[:i], append(Queue{order}, (*queue)[i:]...)...)
appended = true
break
}
}
if !appended {
*queue = append(*queue, order)
}
}
}

The example output after the add method is invoked with the order parameter is as follows. The order is checked to see whether or not it exists in the queue. The order is then appended to the queue:

Let's take a look at the Main method in the next section.