conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/") if err != nil { log.Fatalf("Failed to connect to RabbitMQ: %v", err) } defer conn.Close() ch, err := conn.Channel() if err != nil { log.Fatalf("Failed to open a channel: %v", err) } defer ch.Close() queue, err := ch.QueueDeclare( "myQueue", // name false, // durable false, // delete when unused false, // exclusive false, // no-wait nil, // arguments ) if err != nil { log.Fatalf("Failed to declare a queue: %v", err) } msgs, err := ch.Consume( queue.Name, // queue "", // consumer true, // auto-ack false, // exclusive false, // no-local false, // no-wait nil, // args ) if err != nil { log.Fatalf("Failed to register a consumer: %v", err) } for msg := range msgs { log.Printf("Received a message: %s", msg.Body) }
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/") if err != nil { log.Fatalf("Failed to connect to RabbitMQ: %v", err) } defer conn.Close() ch, err := conn.Channel() if err != nil { log.Fatalf("Failed to open a channel: %v", err) } defer ch.Close() err = ch.ExchangeDeclare( "myExchange", // name "topic", // type true, // durable false, // auto-deleted false, // internal false, // no-wait nil, // arguments ) if err != nil { log.Fatalf("Failed to declare an exchange: %v", err) } err = ch.Publish( "myExchange", // exchange "myKey", // routing key false, // mandatory false, // immediate amqp.Publishing{ ContentType: "text/plain", Body: []byte("Hello, world!"), }, ) if err != nil { log.Fatalf("Failed to publish a message: %v", err) }In this example, we first connect to a RabbitMQ server and open a channel. We then declare an exchange named "myExchange" with type "topic". Finally, we publish a message with routing key "myKey" to the exchange with the text "Hello, world!".