This is a simple package to help you consume messages from AWS SQS.
- Consume messages in parallel
- Consume messages from different defined queues
- Consume messages from different queues by a prefix
- Error handling
- Message unmarshalling
- Message deletion
- Logging
This library requires Go 1.25 or newer. To install the package, use the following command:
go get github.com/inaciogu/go-sqspackage main
import (
"context"
"log"
gosqs "github.com/inaciogu/go-sqs"
)
func main() {
client, err := gosqs.NewConsumer(func(ctx context.Context, msg *gosqs.Message) error {
return nil
}, gosqs.ConsumerOptions{
QueueName: "test_queue",
})
if err != nil {
log.Fatal(err)
}
if err := client.Run(context.Background()); err != nil {
log.Fatal(err)
}
}If you want a more idiomatic, context-first interface, you can use gosqs.NewConsumer.
Return nil from the handler to delete the message, or return gosqs.ErrDrop to delete it without retrying. Any other error will trigger the retry/backoff path.
package main
import (
"context"
"log"
gosqs "github.com/inaciogu/go-sqs"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client, err := gosqs.NewConsumer(func(ctx context.Context, message *gosqs.Message) error {
return nil
}, gosqs.ConsumerOptions{
QueueName: "test_queue",
})
if err != nil {
log.Fatal(err)
}
if err := client.Run(ctx); err != nil {
log.Fatal(err)
}
}If you want to run multiple consumers in parallel, use gosqs.RunAll:
package main
import (
"context"
"log"
gosqs "github.com/inaciogu/go-sqs"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
consumer1, err := gosqs.NewConsumer(func(ctx context.Context, message *gosqs.Message) error {
return nil
}, gosqs.ConsumerOptions{
QueueName: "test_queue_1",
})
if err != nil {
log.Fatal(err)
}
consumer2, err := gosqs.NewConsumer(func(ctx context.Context, message *gosqs.Message) error {
return nil
}, gosqs.ConsumerOptions{
QueueName: "test_queue_2",
})
if err != nil {
log.Fatal(err)
}
if err := gosqs.RunAll(ctx, consumer1, consumer2); err != nil {
log.Fatal(err)
}
}If you want to consume queues by a prefix, you can just set the PrefixBased option to true Then, the QueueName will be used as a prefix to find all queues that match the prefix.
To give the package access to your AWS account, you can use the following environment variables:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEYIf you want to contribute to the development of this package, follow these steps:
- Fork the repository
- Create a new branch (git checkout -b feature/new-feature)
- Commit your changes (git commit -m 'Add new feature')
- Push to the branch (git push origin feature/new-feature)
- Open a Pull Request
To use this package locally (without using your own AWS account) you can execute the docker compose up command that will run the localstack and execute terraform commands to deploy the infra configured in /iac/terraform/main.tf locally.
This package is distributed under the MIT license. See the LICENSE file for more information.
Gustavo Inacio - Linkedin