How to Use Message Queues (NATS, RabbitMQ, Kafka) in Go

Web
Connect to NATS, RabbitMQ, or Kafka in Go by installing their respective third-party client libraries and using their connection methods.

Use third-party client libraries like nats.go, amqp, or sarama to connect to NATS, RabbitMQ, or Kafka respectively, as Go's standard library does not include native message queue support. Install the NATS client and connect to a server with the following code:

import "github.com/nats-io/nats.go"

nc, err := nats.Connect(nats.DefaultURL)
if err != nil {
    panic(err)
}
nc.Publish("subject", []byte("Hello Go!"))
nc.Flush()
nc.Close()

For RabbitMQ, use github.com/rabbitmq/amqp091-go and for Kafka, use github.com/IBM/sarama with similar connection patterns.