'Golang RabbitMQ

I followed the tutorial examples, but it reads all message from queue once, how can I read only one message from a queue? Appreciate!

messages, err := channelRabbitMQ.Consume(
        "QueueService1", // queue name
        "",              // consumer
        true,            // auto-ack
        false,           // exclusive
        false,           // no local
        false,           // no wait
        nil,             // arguments
    )
    if err != nil {
        log.Println(err)
    }
    msg := <-messages
    fmt.Println(string(msg.Body))


Solution 1:[1]

You can use Qos to configure channel to only receive 1 message at a time

url := "..."
queue := "..."
conn, err := amqp.Dial(url)
if err != nil {
  log.Fatal("Cannot connect to rabbitmq")
}

ch, err := conn.Channel()
if err != nil {
  log.Fatal("Cannot create channel")
}

if _, err := ch.QueueDeclare(queue, false, true, false, false, nil); err != nil {
  log.Fatal("Cannot create queue")
}

// Indicate we only want 1 message to acknowledge at a time.
if err := ch.Qos(1, 0, false); err != nil {
  log.Fatal("Qos Setting was unsuccessfull")
}

// Exclusive consumer
messages, err := ch.Consume(queue, "", false, true, false, false, nil)

msg := <-messages
fmt.Println(string(msg.Body))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chandan