amqp091-go
An AMQP 0-9-1 Go client maintained by the RabbitMQ team. Originally by @streadway: `streadway/amqp`
Top Related Projects
Go client for AMQP 0.9.1
Confluent's Apache Kafka Golang client
Sarama is a Go library for Apache Kafka.
Kafka library in Go
Golang client for NATS, the cloud native messaging system.
Quick Overview
The rabbitmq/amqp091-go repository is a Go client library for the AMQP 0-9-1 protocol, specifically designed for use with RabbitMQ. It provides a robust and efficient way to interact with RabbitMQ message brokers, allowing developers to implement messaging patterns in their Go applications.
Pros
- Native Go implementation, ensuring good performance and compatibility
- Supports both synchronous and asynchronous communication patterns
- Comprehensive coverage of AMQP 0-9-1 protocol features
- Active maintenance and community support
Cons
- Limited to AMQP 0-9-1 protocol, not supporting newer AMQP versions
- Requires understanding of AMQP concepts for effective use
- May have a steeper learning curve compared to simpler messaging libraries
Code Examples
- Connecting to RabbitMQ:
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatalf("Failed to connect to RabbitMQ: %v", err)
}
defer conn.Close()
- Publishing a message:
ch, _ := conn.Channel()
defer ch.Close()
err = ch.Publish(
"exchange_name", // exchange
"routing_key", // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
Body: []byte("Hello, RabbitMQ!"),
})
- Consuming messages:
msgs, err := ch.Consume(
"queue_name", // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
for msg := range msgs {
log.Printf("Received a message: %s", msg.Body)
}
Getting Started
To use the amqp091-go library in your Go project:
-
Install the library:
go get github.com/rabbitmq/amqp091-go
-
Import the library in your Go code:
import amqp "github.com/rabbitmq/amqp091-go"
-
Establish a connection to RabbitMQ:
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/") if err != nil { log.Fatalf("Failed to connect to RabbitMQ: %v", err) } defer conn.Close()
-
Create a channel and start using AMQP operations like declaring exchanges, queues, publishing, and consuming messages.
Competitor Comparisons
Go client for AMQP 0.9.1
Pros of amqp
- More established and widely used in production environments
- Extensive documentation and community support
- Broader feature set, including support for additional AMQP operations
Cons of amqp
- No longer actively maintained (last commit in 2021)
- May lack support for newer RabbitMQ features
- Potential performance limitations compared to amqp091-go
Code Comparison
amqp:
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
ch, err := conn.Channel()
q, err := ch.QueueDeclare("hello", false, false, false, false, nil)
err = ch.Publish("", q.Name, false, false, amqp.Publishing{ContentType: "text/plain", Body: []byte("Hello World!")})
amqp091-go:
conn, err := amqp091.Dial("amqp://guest:guest@localhost:5672/")
ch, err := conn.Channel()
q, err := ch.QueueDeclare("hello", false, false, false, false, nil)
err = ch.PublishWithContext(context.Background(), "", q.Name, false, false, amqp091.Publishing{ContentType: "text/plain", Body: []byte("Hello World!")})
The main difference in the code is the use of PublishWithContext
in amqp091-go, which allows for better context handling and cancellation support.
Confluent's Apache Kafka Golang client
Pros of confluent-kafka-go
- Built specifically for Apache Kafka, offering optimized performance and features
- Supports advanced Kafka-specific functionalities like transactions and exactly-once semantics
- Provides a more comprehensive set of Kafka-related tools and integrations
Cons of confluent-kafka-go
- Limited to Kafka messaging, less versatile for other messaging protocols
- May have a steeper learning curve for developers not familiar with Kafka concepts
- Potentially more complex setup and configuration compared to simpler AMQP libraries
Code Comparison
confluent-kafka-go:
producer, err := kafka.NewProducer(&kafka.ConfigMap{"bootstrap.servers": "localhost"})
err = producer.Produce(&kafka.Message{
TopicPartition: kafka.TopicPartition{Topic: &topic, Partition: kafka.PartitionAny},
Value: []byte(value),
}, nil)
amqp091-go:
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
ch, err := conn.Channel()
err = ch.Publish(
"exchange_name", "routing_key", false, false,
amqp.Publishing{ContentType: "text/plain", Body: []byte(message)},
)
The code snippets demonstrate the different approaches to producing messages, with confluent-kafka-go focusing on Kafka-specific concepts like topics and partitions, while amqp091-go uses AMQP terminology like exchanges and routing keys.
Sarama is a Go library for Apache Kafka.
Pros of Sarama
- More comprehensive Kafka support, including producer, consumer, and admin client functionalities
- Better performance and scalability for high-throughput Kafka applications
- Active development with frequent updates and a large community
Cons of Sarama
- Steeper learning curve due to more complex API and features
- Specific to Kafka, not suitable for other message brokers
- Requires more configuration and setup compared to simpler AMQP clients
Code Comparison
amqp091-go (RabbitMQ):
conn, _ := amqp.Dial("amqp://guest:guest@localhost:5672/")
ch, _ := conn.Channel()
_ = ch.Publish("exchange", "routing_key", false, false, amqp.Publishing{
ContentType: "text/plain",
Body: []byte("Hello, RabbitMQ!"),
})
Sarama (Kafka):
config := sarama.NewConfig()
producer, _ := sarama.NewSyncProducer([]string{"localhost:9092"}, config)
_, _, _ = producer.SendMessage(&sarama.ProducerMessage{
Topic: "my-topic",
Value: sarama.StringEncoder("Hello, Kafka!"),
})
The code examples show that Sarama requires more setup for Kafka-specific configurations, while amqp091-go offers a simpler interface for RabbitMQ operations. Sarama provides more control over Kafka-specific features, but at the cost of increased complexity.
Kafka library in Go
Pros of kafka-go
- Native Go implementation, optimized for performance and concurrency
- Supports both consumer groups and manual partition assignment
- Comprehensive feature set, including offset management and compression
Cons of kafka-go
- Steeper learning curve due to Kafka's complex architecture
- Less mature compared to amqp091-go, potentially more bugs or edge cases
- Requires more setup and configuration for basic usage
Code Comparison
amqp091-go:
conn, _ := amqp.Dial("amqp://guest:guest@localhost:5672/")
ch, _ := conn.Channel()
_ = ch.Publish(
"exchange", "routing_key", false, false,
amqp.Publishing{ContentType: "text/plain", Body: []byte("Hello")},
)
kafka-go:
w := kafka.NewWriter(kafka.WriterConfig{
Brokers: []string{"localhost:9092"},
Topic: "topic",
})
_ = w.WriteMessages(context.Background(),
kafka.Message{Value: []byte("Hello")},
)
Both libraries provide Go-native implementations for their respective messaging systems. amqp091-go offers a simpler API for basic RabbitMQ operations, while kafka-go provides more flexibility and features specific to Kafka's distributed architecture. The choice between them depends on the specific messaging requirements and scalability needs of your project.
Golang client for NATS, the cloud native messaging system.
Pros of nats.go
- Simpler, lightweight protocol with lower latency
- Built-in support for various messaging patterns (pub/sub, request-reply, etc.)
- Better scalability and easier clustering
Cons of nats.go
- Less mature ecosystem compared to AMQP
- Fewer advanced features like message persistence and complex routing
- Limited support for transactional operations
Code Comparison
nats.go:
nc, _ := nats.Connect(nats.DefaultURL)
nc.Publish("foo", []byte("Hello World"))
nc.Subscribe("foo", func(m *nats.Msg) {
fmt.Printf("Received a message: %s\n", string(m.Data))
})
amqp091-go:
conn, _ := amqp.Dial("amqp://guest:guest@localhost:5672/")
ch, _ := conn.Channel()
q, _ := ch.QueueDeclare("hello", false, false, false, false, nil)
ch.Publish("", q.Name, false, false, amqp.Publishing{
ContentType: "text/plain",
Body: []byte("Hello World"),
})
The nats.go code is more concise and straightforward, reflecting its simpler protocol. The amqp091-go code requires more setup and configuration, but offers more control over message properties and routing.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Go RabbitMQ Client Library
This is a Go AMQP 0.9.1 client maintained by the RabbitMQ core team. It was originally developed by Sean Treadway.
Differences from streadway/amqp
Some things are different compared to the original client, others haven't changed.
Package Name
This library uses a different package name. If moving from streadway/amqp
,
using an alias may reduce the number of changes needed:
amqp "github.com/rabbitmq/amqp091-go"
License
This client uses the same 2-clause BSD license as the original project.
Public API Evolution
This client retains key API elements as practically possible. It is, however, open to reasonable breaking public API changes suggested by the community. We don't have the "no breaking public API changes ever" rule and fully recognize that a good client API evolves over time.
Project Maturity
This project is based on a mature Go client that's been around for over a decade.
Supported Go Versions
This client supports two most recent Go release series.
Supported RabbitMQ Versions
This project supports RabbitMQ versions starting with 2.0
but primarily tested
against currently supported RabbitMQ release series.
Some features and behaviours may be server version-specific.
Goals
Provide a functional interface that closely represents the AMQP 0.9.1 model targeted to RabbitMQ as a server. This includes the minimum necessary to interact the semantics of the protocol.
Non-goals
Things not intended to be supported.
- Auto reconnect and re-synchronization of client and server topologies.
- Reconnection would require understanding the error paths when the topology cannot be declared on reconnect. This would require a new set of types and code paths that are best suited at the call-site of this package. AMQP has a dynamic topology that needs all peers to agree. If this doesn't happen, the behavior is undefined. Instead of producing a possible interface with undefined behavior, this package is designed to be simple for the caller to implement the necessary connection-time topology declaration so that reconnection is trivial and encapsulated in the caller's application code.
- AMQP Protocol negotiation for forward or backward compatibility.
- 0.9.1 is stable and widely deployed. AMQP 1.0 is a divergent specification (a different protocol) and belongs to a different library.
- Anything other than PLAIN and EXTERNAL authentication mechanisms.
- Keeping the mechanisms interface modular makes it possible to extend outside of this package. If other mechanisms prove to be popular, then we would accept patches to include them in this package.
- Support for
basic.return
andbasic.ack
frame ordering. This client uses Go channels for certain protocol events and ordering between events sent to two different channels generally cannot be guaranteed.
Usage
See the _examples subdirectory for simple producers and consumers executables. If you have a use-case in mind which isn't well-represented by the examples, please file an issue.
Documentation
Contributing
Pull requests are very much welcomed. Create your pull request on a non-main branch, make sure a test or example is included that covers your change, and your commits represent coherent changes that include a reason for the change.
See CONTRIBUTING.md for more information.
License
BSD 2 clause, see LICENSE for more details.
Top Related Projects
Go client for AMQP 0.9.1
Confluent's Apache Kafka Golang client
Sarama is a Go library for Apache Kafka.
Kafka library in Go
Golang client for NATS, the cloud native messaging system.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot