/go-rabbitmq

Golang AMQP wrapper for RabbitMQ with better API

Primary LanguageGoMozilla Public License 2.0MPL-2.0

go-rabbitmq

Golang AMQP wrapper for RabbitMQ with better API

Table of Contents

Background

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.

Features

  • 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.

Usage

Installation

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"
)

Connect to 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()

Declare Queue

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)
}

Declare Exchange

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)
}

Bind Queue to Exchange

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)
}

Publish a Message

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)
}

Consume Messages

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

How It Works

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.

License

This project is under Mozilla Public License 2.0.

Contributing

Realy love any contribution. Feel free to create a Pull Request with following this commit convention.