Go's standard library has no client for Kafka, RabbitMQ, NATS or any other broker. It gives you the foundations instead: net for sockets, net/http for HTTP-based brokers, encoding/json for payloads, and goroutines, channels and context for consumers. For real brokers you add a client module, and for in-process queues channels are often enough.
This guide shows what the standard library covers, how to use broker clients idiomatically, and the delivery patterns that keep messages from being lost or processed twice.
| Task | Package |
|---|---|
| Talk to brokers with an HTTP API | net/http |
| Raw TCP connections to a broker | net, crypto/tls |
| Encode and decode messages | encoding/json, encoding/gob, encoding/binary |
| In-process queues and workers | channels, sync, context |
| Retry timing and timeouts | time, context |
| Structured logging of message handling | log/slog |
Protocol buffers are not in the standard library either. Use google.golang.org/protobuf when your messages are protobuf.
When producers and consumers live in the same program, a buffered channel is a message queue. It gives back-pressure for free: when the buffer is full, producers block.
This queue lives in memory, so messages are lost if the process crashes. Use a real broker when you need durability, multiple services or replay.
Some brokers and cloud queues expose REST endpoints. Use http.NewRequestWithContext with a deadline, set the content type, and always check the status code.
Do not use os/exec to call a broker's command-line tool from application code. It is slow, hard to secure and loses error detail. Use the broker's client library or HTTP API.
Each broker has a well-known Go client. They all follow the same shape: connect, publish or subscribe, handle messages, close.
| Broker | Common Go client |
|---|---|
| RabbitMQ | github.com/rabbitmq/amqp091-go |
| Apache Kafka | github.com/segmentio/kafka-go, github.com/twmb/franz-go, or github.com/IBM/sarama |
| NATS and JetStream | github.com/nats-io/nats.go |
| Redis streams and lists | github.com/redis/go-redis/v9 |
| AWS SQS and SNS | github.com/aws/aws-sdk-go-v2 |
| Google Pub/Sub | cloud.google.com/go/pubsub |
A NATS example shows the typical flow:
Plain NATS is at-most-once. For persistence and acknowledgements, use JetStream. Kafka and RabbitMQ provide durability and acknowledgements in their own ways.
Brokers typically offer at-least-once delivery, which means a message can arrive more than once after a retry or crash. Plan for it.
version field or a protobuf definition.signal.NotifyContext ties shutdown to the process signals and gives you a context to pass into consumers.
This needs the context, os, os/signal and syscall imports. Consumers should check ctx.Done() in their loop so in-flight messages complete cleanly.
Not for external messaging. Channels act as an in-memory queue inside one process, but they have no persistence or network transport.
segmentio/kafka-go is easy to start with, and franz-go is feature-rich and fast. Pick one that is actively maintained and fits your needs for transactions and consumer groups.
Assume duplicates will happen and make the handler idempotent, for example by recording the message ID in the same database transaction as the result.
Use channels inside a single process. Use a broker when messages must survive restarts or move between services.
The standard library supplies the transport, encoding and concurrency pieces, while broker clients provide the protocol. Use channels for in-process queues, official or well-known clients for Kafka, RabbitMQ and NATS, and design consumers to be idempotent with timeouts, retries and graceful shutdown.