-3
from Queue import Queue

q = Queue()

def check():
  while True:
     user = q.get()
     req = makeRequest(url + user)

def main():
  for i in user_list: q.put(i)

Here is my python code that loops through a text file of usernames and adds it to a queue. the queue is snatched one by one and is used to make an http request to an api, i want to do the same thing in golang, I read the documentation for the Queue package but im struggling to wrap my head around it, any help is appreciated.

1 Answers1

0

The simplest way to implement the queue data structure in Golang is to use a slice. Since a queue follows a FIFO (First-In-First-Out) structure, the dequeue and enqueue operations can be performed as follows:

Use the built-in append function to enqueue. Slice off the first element to dequeue.

Here is a Program with the logic stated above:

package main

import "fmt"

func enqueue(queue []int, element int) []int {
    queue = append(queue, element) // Simply append to enqueue.
    fmt.Println("Enqueued:", element)
    return queue
}

func dequeue(queue []int) []int {
    element := queue[0] // The first element is the one to be dequeued.
    fmt.Println("Dequeued:", element)
    return queue[1:] // Slice off the element once it is dequeued.
}

func main() {
    var queue []int // Make a queue of ints.

    queue = enqueue(queue, 10)
    queue = enqueue(queue, 20)
    queue = enqueue(queue, 30)

    fmt.Println("Queue:", queue)

    queue = dequeue(queue)
    fmt.Println("Queue:", queue)

    queue = enqueue(queue, 40)
    fmt.Println("Queue:", queue)
}

Output:

Enqueued: 10
Enqueued: 20
Enqueued: 30
Queue: [10 20 30]
Dequeued: 10
Queue: [20 30]
Enqueued: 40
Queue: [20 30 40]
Gopher
  • 721
  • 3
  • 8