Introduction
Configuration
Language Support
AMQP 0-9-1 Overview
More Exchange Types
More Consumer Features
Queue Deep-dive
Other Features
Reliable Message Delivery
High Availability
Monitoring
Management HTTP API
Tutorials
Networking
LavinMQ CLI
LavinMQ with Golang
The recommended library for Golang to access LavinMQ server is amqp091-go.
Before we jump right into writing our code, let’s set up the development environment.
Go development environment
- Download and install Go. Confirm that it’s available from your command line:
go version
- Create a directory for your project and navigate into it:
mkdir go_sample && cd go_sample
- Enable dependency tracking for your code:
go mod init go_sample
Publishing messages
In your project directory, create a file producer.go
and add the snippet
below to it.
# producer.go
package main
import (
"context"
"log"
"time"
amqp "github.com/rabbitmq/amqp091-go"
)
func failOnError(err error, msg string) {
if err != nil {
log.Panicf("%s: %s", msg, err)
}
}
func main() {
connection, err := amqp.Dial("lavinmq server URL")
failOnError(err, "Failed to connect to LavinMQ")
defer connection.Close()
channel, err := connection.Channel()
failOnError(err, "Failed to open a channel")
log.Print("[✅] Connection over channel established")
defer channel.Close()
queue, err := channel.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()
body := "Hello World"
err = channel.PublishWithContext(ctx,
"", // exchange
queue.Name, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
Body: []byte(body),
})
failOnError(err, "Failed to publish a message")
log.Printf("[📥] Message sent to queue: %s\n", body)
}
In the snippet above, we first of all imported a couple of packages - the amqp Go client library included. We then established a connection to the LavinMQ server over a channel. Finally, we created a queue and published a “Hello world” message to the queue.
Note: Remember to replace “lavinmq server URL” in the snippet above with your correct URL.
Before running the producer, first, run go mod tidy
in your terminal - This way, Go will
add the amqp091-go
module as a requirement, as well as a go.sum
file for use in authenticating
the module.
You can now run the producer with go run producer.go
Consume messages
In your project directory, create a file consumer.go
and add the snippet
below to it.
package main
import (
"log"
amqp "github.com/rabbitmq/amqp091-go"
)
func failOnError(err error, msg string) {
if err != nil {
log.Panicf("%s: %s", msg, err)
}
}
func main() {
connection, err := amqp.Dial("Lavinmq server URL")
failOnError(err, "Failed to connect to LavinMQ")
defer connection.Close()
channel, err := connection.Channel()
failOnError(err, "Failed to open a channel")
log.Print("[✅] Connection over channel established")
defer channel.Close()
q, err := channel.QueueDeclare(
"hello", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
failOnError(err, "Failed to declare a queue")
msgs, err := channel.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("[✅] Message Received: %s", d.Body)
}
}()
log.Printf("[❎] Waiting for messages. To exit press CTRL+C")
<-forever
}
Note: Again, remember to replace “lavinmq server URL” in the snippet above with your correct URL.
Run the consumer with go run consumer.go
Ready to take the next steps? Here are some things you should keep in mind:
Managed LavinMQ instance on CloudAMQP
LavinMQ has been built with performance and ease of use in mind - we've benchmarked a throughput of about 1,000,000 messages/sec. You can try LavinMQ without any installation hassle by creating a free instance on CloudAMQP. Signing up is a breeze.
Help and feedback
We welcome your feedback and are eager to address any questions you may have about this piece or using LavinMQ. Join our Slack channel to connect with us directly. You can also find LavinMQ on GitHub.