Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596package amqpx_test
import ( "context" "log" "log/slog" "os" "os/signal"
amqpx "github.com/nemith/amqpx" amqp "github.com/rabbitmq/amqp091-go")
func ExampleProducer() { ctx := context.Background() client, err := amqpx.NewClient("amqp://localhost:5672") if err != nil { slog.Error("failed to create client", "err", err) } defer client.Close()
producer, err := client.NewProducer(ctx, amqpx.WithExchange("my-exchange"), amqpx.WithDeclaration( &amqpx.ExchangeDeclaration{ Name: "my-exchange", Kind: "direct", }, ), ) if err != nil { slog.Error("failed to create producer", "err", err) return } defer producer.Close()
msg := amqp.Publishing{ Body: []byte("Hello, World!"), } if err := producer.Publish(context.Background(), "key", msg); err != nil { slog.Error("failed to publish msg", "err", err) return }}
func ExampleConsumer() { ctx := context.Background() client, err := amqpx.NewClient("amqp://localhost:5672") if err != nil { slog.Error("failed to create client", "err", err) } defer client.Close()
consumer, err := client.NewConsumer(ctx, amqpx.WithQueue("my-queue"), amqpx.WithAutoAck(true), amqpx.WithDeclaration( &amqpx.ExchangeDeclaration{ Name: "my-exchange", Kind: "direct", }, &amqpx.QueueDeclaration{Name: "my-queue"}, &amqpx.QueueBinding{ Queue: "my-queue", Exchange: "my-exchange", Key: "key", }, ), ) if err != nil { slog.Error("failed to create consumer", "err", err) return }
defer consumer.Close()
msgs, err := consumer.Consume(ctx) if err != nil { slog.Error("failed to consume msgs", "err", err) return }
stop := make(chan os.Signal, 1) signal.Notify(stop, os.Interrupt)
Loop: for { select { case msg := <-msgs: log.Printf("received msg: %s", msg.Body) case <-stop: break Loop } }}