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", "direct", true, false, false, false, nil, ) if err != nil { log.Fatalf("Failed to declare exchange: %v", err) } body := "Hello World!" err = ch.Publish( "myExchange", "myRoutingKey", false, false, amqp.Publishing{ ContentType: "text/plain", Body: []byte(body), }, ) if err != nil { log.Fatalf("Failed to publish message: %v", err) }
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", "fanout", true, false, false, false, nil, ) if err != nil { log.Fatalf("Failed to declare exchange: %v", err) } queue, err := ch.QueueDeclare( "", false, false, true, false, nil, ) if err != nil { log.Fatalf("Failed to declare queue: %v", err) } err = ch.QueueBind( queue.Name, "", "myExchange", false, nil, ) if err != nil { log.Fatalf("Failed to bind queue: %v", err) } body := "Hello World!" err = ch.Publish( "myExchange", "", false, false, amqp.Publishing{ ContentType: "text/plain", Body: []byte(body), }, ) if err != nil { log.Fatalf("Failed to publish message: %v", err) }This example demonstrates how to declare a fanout exchange, declare a queue, bind the queue to the exchange, and publish a message to the exchange without using a routing key. The message will be delivered to all queues bound to the exchange.