// Import necessary packages
package main
import (
"log"
"github.com/streadway/amqp"
"google.golang.org/protobuf/proto"
pb "path/to/your/protobuf/package" // Import your generated Protobuf package
)
func main() {
// Create a connection to RabbitMQ
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatal("Failed to connect to RabbitMQ:", err)
}
defer conn.Close()
// Create a channel
ch, err := conn.Channel()
if err != nil {
log.Fatal("Failed to open a channel:", err)
}
defer ch.Close()
// Declare a queue
q, err := ch.QueueDeclare(
"your_queue_name", // queue name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
if err != nil {
log.Fatal("Failed to declare a queue:", err)
}
// Create a Protobuf message
message := &pb.YourMessageType{
Field1: "value1",
Field2: 100,
}
// Serialize the Protobuf message to binary format
data, err := proto.Marshal(message)
if err != nil {
log.Fatal("Failed to marshal Protobuf message:", err)
}
// Publish the message to the queue
err = ch.Publish(
"", // exchange
q.Name, // routing key (queue name)
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "application/octet-stream",
Body: data,
})
if err != nil {
log.Fatal("Failed to publish a message:", err)
}
log.Println("Message published successfully!")
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?