skip to content
Alvin Lucillo

Auto-acknowledge message

/ 1 min read

Auto acknowledging has a caveat: since a message is automatically acknowledged once published, the message won’t be in the queue anymore if there’s an issue with the consumer.

	msgs, err := ch.Consume(
		q.Name, // queue
		"",     // consumer
		true,  // auto-ack
		false,  // exclusive
		false,  // no-local
		false,  // no-wait
		nil,    // args
	)
	failOnError(err, "Failed to register a consumer")

	var forever chan struct{}

	go func() {
		for d := range msgs {
			log.Printf("Received a message: %s", d.Body)
			dotCount := bytes.Count(d.Body, []byte("."))
			t := time.Duration(dotCount)
			time.Sleep(t * time.Second)
			log.Printf("Error occurred")
		}
	}()

We ran the publisher Go program that sent the ‘hello’ payload

go run .
2026/01/12 21:02:20  [x] Sent hello

Then we ran the worker (with snippet from above) that received the same message payload. A simulated error happened. But when we restart the client, the message is no longer there to be retried.

go run ./worker
2026/01/12 21:02:18  [*] Waiting for messages. To exit press CTRL+C
2026/01/12 21:02:20 Received a message: hello
2026/01/12 21:02:20 Error occurred
^Csignal: interrupt

go run ./worker
2026/01/12 21:02:25  [*] Waiting for messages. To exit press CTRL+C