Build Status codecov Go Report Card Sourcegraph HitCount GoDoc License


This package provides an ability to encapsulate creation and configuration of RabbitMQ([AMQP])( entities like queues, exchanges, producers and consumers in a declarative way with a single config.

Exchanges, queues and producers are going to be initialized in the background.

go-mq supports both sync and async producers.

go-mq has auto reconnects on closed connection or network error. You can configure delay between each connect try using reconnect_delay option.


go get -u


Visit godoc to get information about library API.

For those of us who preferred learn something new on practice there is working examples in example directory.


You can configure mq using mq.Config struct directly or by filling it from config file.

Supported configuration tags:

  • yaml
  • json
  • mapstructure

Available options:

dsn: "amqp://login:[email protected]:port/virtual_host"
reconnect_delay: 5s                     # Interval between connection tries. Check for details.
test_mode: false                        # Switches library to use mocked broker. Defaults to false.
  - name: "exchange_name"
    type: "direct"
      # Available options with default values:
      auto_delete: false
      durable: false
      internal: false
      no_wait: false
  - name: "queue_name"
    exchange: "exchange_name"
    routing_key: "route"
    # A set of arguments for the binding.
    # The syntax and semantics of these arguments depend on the exchange class.
      no_wait: false
    # Available options with default values:
      auto_delete: false
      durable: false
      exclusive: false
      no_wait: false
  - name: "producer_name"
    buffer_size: 10                      # Declare how many messages we can buffer during fat messages publishing.
    exchange: "exchange_name"
    routing_key: "route"
    sync: false                          # Specify whether producer will worked in sync or async mode.
    # Available options with default values:
      content_type:  "application/json"
      delivery_mode: 2                   # 1 - non persistent, 2 - persistent.
  - name: "consumer_name"
    queue: "queue_name"
    workers: 1                           # Workers count. Defaults to 1.
    prefetch_count: 0                    # Prefetch message count per worker.
    prefetch_size: 0                     # Prefetch message size per worker.
    # Available options with default values:
      no_ack: false
      no_local: false
      no_wait: false
      exclusive: false

Error handling

All errors are accessible via exported channel:

package main

import (


func main() {
	config := mq.Config{} // Set your configuration.
	queue, _ := mq.New(config)
	// ...

	go handleMQErrors(queue.Error())
	// Other logic.

func handleMQErrors(errors <-chan error) {
	for err := range errors {

If channel is full – new errors will be dropped.

Errors from sync producer won’t be accessible from error channel because they returned directly.


There are some cases that can only be tested with real broker and some cases that can only be tested with mocked broker.

If you are able to run tests with a real broker run them with:

go test -mock-broker=0

Otherwise mock will be used.


Check releases page.

How to upgrade

From version 0.x to 1.x

  • GetConsumer() method was renamed to Consumer(). This is done to follow go guideline.

  • GetProducer() method was removed. Use instead AsyncProducer() or SyncProducer() if you want to catch net error by yourself.


Feel free to create issues with bug reports or your wishes.