As far as I can tell from the docs, yes, "preferred" does mean "recommended".
It seems that channel.Get()
doesn't provide as many features as channel.Consume()
, as well as being more readily usable in concurrent code due to it's returning a chan
of Delivery
, as opposed to each individual Delivery
separately.
The extra features mentioned are exclusive
, noLocal
and noWait
, as well as an optional Table
of args "that have specific semantics for the queue or server."
To implement a Pop()
function using channel.Consume()
you could, to link to some code fragments from the amqp example consumer, create a channel using the Consume()
function, create a function to handle the chan
of Delivery
which will actually implement your Pop()
functionality, then fire off the handle()
func in a goroutine
.
The key to this is that the channel (in the linked example) will block on sending if nothing is receiving. In the example, the handle()
func uses range
to process the entire channel until it's empty. Your Pop()
functionality may be better served by a function that just receives the last value from the chan
and returns it. Every time it's run it will return the latest Delivery
.
EDIT: Example function to receive the latest value from the channel and do stuff with it (This may not work for your use case, it may be more useful if the function sent the Delivery
on another chan
to another function to be processed. Also, I haven't tested the code below, it may be full of errors)
func handle(deliveries <-chan amqp.Delivery, done chan error) {
select {
case d = <-deliveries:
// Do stuff with the delivery
// Send any errors down the done chan. for example:
// done <- err
default:
done <- nil
}
}