skip to content
Alvin Lucillo

Simple example of rabbitmq trx with Go

/ 3 min read

Go has a module that you can use to integrate rabbitmq with your system: github.com/rabbitmq/amqp091-go. The examples below are from rabbitmq.com. Be sure that you have a running rabbitmq instance on your local machine, either as a Docker container or running process on your machine.

The example below is a basic publishing and consuming of a message. When a sender publishes a message to a queue, the message is available from a receiver to consume. Note that the example uses the default exchange "".

Sample run:

  go run .
2026/01/11 20:22:06  [x] Sent Hello World!
  go run ./receiver/
2026/01/11 20:22:13  [*] Waiting for messages. To exit press CTRL+C
2026/01/11 20:22:13 Received a message: Hello World!

sender.go

func failOnError(err error, msg string) {
	if err != nil {
		log.Panicf("%s: %s", msg, err)
	}
}

func main() {
	// connects to your rabbitmq instance
	conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
	failOnError(err, "Failed to connect to RabbitMQ")
	defer conn.Close()

	ch, err := conn.Channel()
	failOnError(err, "Failed to open a channel")
	defer ch.Close()

	// creates or returns the queue
	// same with: `rabbitmqadmin declare queue`
	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")
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	// publishes a message to the queue
	// same with: `rabbitmqadmin publish message`
	// note here that it's using the default exchange with empty value in the first argument
	body := "Hello World!"
	err = ch.PublishWithContext(ctx,
		"",     // exchange
		q.Name, // routing key
		false,  // mandatory
		false,  // immediate
		amqp.Publishing{
			ContentType: "text/plain",
			Body:        []byte(body),
		})
	failOnError(err, "Failed to publish a message")
	log.Printf(" [x] Sent %s\n", body)
}

receiver.go

func failOnError(err error, msg string) {
	if err != nil {
		log.Panicf("%s: %s", msg, err)
	}
}

func main() {
	conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
	failOnError(err, "Failed to connect to RabbitMQ")
	defer conn.Close()

	ch, err := conn.Channel()
	failOnError(err, "Failed to open a channel")
	defer ch.Close()

	// creates or declares a queue
	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")

	// returns a channel for the messages
	msgs, err := ch.Consume(
		q.Name, // queue
		"",     // consumer
		true,   // auto-ack
		false,  // exclusive
		false,  // no-local
		false,  // no-wait
		nil,    // args
	)
	failOnError(err, "Failed to register a consumer")

	var forever chan struct{}

	go func() {
		for d := range msgs {
			log.Printf("Received a message: %s", d.Body)
		}
	}()

	log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
	<-forever
}

Reference