Golang AMQP wrapper for RabbitMQ with better API
In Golang, to use RabbitMQ with AMQP has advantages, especially in messaging systems. It's done with the AMQP connector. But, the problem is it has a less convenient API. Programmers have to write something that should be set by default. For example, when creating a queue on RabbitMQ. We have to do this.
q, err := ch.QueueDeclare(
"hello", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
failOnError(err, "Failed to declare a queue")
Too many false
in there, which should be set as the default value.
By using this module, we can do same think with less code. See above.
q, err := mq.Queue().
WithName("hello").
Declare()
failOnError(err, "Failed to declare a queue")
No need to write false
, because it is the default value.
So, to conclude, this module makes it easy to use amqp for rabbitmq.
- Built on top of famous AMQP connector in Go.
- Connection, channel, queue, or any original objects by the AMQP connector are publicly exposed, so that can be controlled manually.
- It use construction API.
- Reuse connection to create MQ stuff.
Inside terminal emulator, simply run command below.
go get github.com/hadihammurabi/go-rabbitmq
After installation it can be imported into any Go project. For example.
package main
import (
rabbitmq "github.com/hadihammurabi/go-rabbitmq"
)
It can do as below.
mq, err := rabbitmq.New("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatal(err)
}
// don't forget to close the connection and channel
defer mq.Close()
Queue declaration can be done like this, after connecting to mq of course.
It only connects to the queue if the queue exists or create one if it doesn't exist. (RabbitMQ behavior)
q, err := mq.Queue().
WithName("hello").
Declare()
if err != nil {
log.Fatal(err)
}
Exchange declaration can be done like this, after connecting to mq of course.
err := mq.Exchange().
WithName("hello").
WithType(exchange.TypeFanout)).
Declare()
if err != nil {
log.Fatal(err)
}
Every message published to exchange will be distributed to every bound queue. To bind queue with exchange, follow example below.
err := q.Binding().
WithExchange("hello").
Bind()
if err != nil {
log.Fatal(err)
}
A message can be sent to exchange by mentioning the name exchange. Publishing a message can do like this.
err := mq.Publish(
rabbitmq.NewPublishOptions().
SetExchange("hello").
SetMessage(amqp.Publishing{
ContentType: "text/plain",
Body: []byte(body),
}),
)
if err != nil {
log.Fatal(err)
}
Every message in the queue can be consumed by the queue consumer. To consume messages in queue can do like this.
The following code will run forever to listen for new message in queue.
msgs, err := mq.Consumer().Consume()
if err != nil {
log.Fatal(err)
}
forever := make(chan bool)
go func() {
for msg := range msgs {
fmt.Println(string(msg.Body))
msg.Ack(false)
}
}()
<-forever
Every AMQP related function call will invoke the AMQP connector function. For example when creating a new MQ, it will call the AMQP connection function. Declare queue, declare exchange, publish, consume, etc will do the same.
Please use any references made previously to prevent too many function calls.
This project is under Mozilla Public License 2.0.
Realy love any contribution. Feel free to create a Pull Request with following this commit convention.