amqp broker in go

To use an AMQP (Advanced Message Queuing Protocol) broker in Go, we can leverage the streadway/amqp package. This package provides us with the functionality to connect, publish, consume messages and more to/from an AMQP broker, with RabbitMQ being the most popular implementation of AMQP.

To connect to a RabbitMQ broker, we can use the Dial function from the streadway/amqp package.

main.go
package main

import (
	"fmt"
	"log"

	"github.com/streadway/amqp"
)

func main() {
	// establish connection
	conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
	if err != nil {
		log.Fatalf("could not connect to RabbitMQ: %v", err)
	}
	defer conn.Close()

	fmt.Println("Connected to RabbitMQ!")
}
308 chars
20 lines

In the above example, we connect to a RabbitMQ broker running on localhost with default credentials (guest:guest).

From here, we can create a channel, declare a queue, publish or consume messages, and many more operations using the conn object.

gistlibby LogSnag