AWS SDK for Go V2 code examples for Amazon SQS
Purpose
These examples demonstrate how to perform several
Amazon Simple Queue Service (Amazon SQS)
operations using version 2 of the AWS SDK for Go.
Prerequisites
You must have an AWS account, and have your default credentials and AWS Region
configured as described in
Configuring the AWS SDK for Go
in the AWS SDK for Go Developer Guide.
Running the code
ChangeMsgVisibility/ChangeMsgVisibilityv2.go
This example sets the visibility timeout for a message in an Amazon SQS queue.
go run ChangeMsgVisibilityv2.go -q QUEUE-NAME -h RECEIPT-HANDLE -v VISIBILITY
- QUEUE-NAME is the name of the queue.
- RECEIPT-HANDLE is the receipt handle of the message.
- VISIBILITY is the duration, in seconds, that the message is not visible to other consumers.
The example ensures the value is between 0 and 12 hours;
the default is 30 seconds.
The unit test accepts similar values in config.json.
This example configures an Amazon SQS queue to use long polling.
go run ConfigureLPQueuev2.go -q QUEUE-NAME [-w WAIT-TIME]
- QUEUE-NAME is the name of the queue to configure.
- WAIT-TIME is how long, in seconds, to wait.
The example ensures the value is between 1 and 20;
the default is 10.
The unit test accepts similar values in config.json.
CreateQueue/CreateQueuev2.go
This example creates an Amazon SQS queue.
go run CreateQueuev2.go -q QUEUE-NAME
- QUEUE-NAME is the name of the queue to create.
The unit test accepts a similar value in config.json.
CreateLPQueue/CreateLPQueuev2.go
This example creates a long-polling Amazon SQS queue.
go run CreateLPQueuev2.go -q QUEUE-NAME [-w WAIT-TIME]
- QUEUE-NAME is the name of the queue to create.
- WAIT-TIME is how long, in seconds, to wait.
The example ensures the value is between 1 and 20;
the default is 10.
The unit test accepts similar values in config.json.
DeadLetterQueue/DeadLetterQueuev2.go
This example configures an Amazon SQS queue for messages
that could not be delivered to another queue.
go run DeadLetterQueuev2.go -q QUEUE-NAME -d DEAD-LETTER-QUEUE-NAME
- QUEUE-NAME is the name of the queue from which the dead letters are sent.
- DEAD-LETTER-QUEUE-NAME is the name of the queue to which the dead letters are sent.
The unit test accepts similar values in config.json.
DeleteMessage/DeleteMessagev2.go
This example deletes a message from an Amazon SQS queue.
go run DeleteMessagev2.go -q QUEUE-NAME -m MESSAGE-HANDLE
- QUEUE-NAME is the name of the queue from which the message is deleted.
- MESSAGE-HANDLE is the handle of the message to delete.
The unit test accepts similar values in config.json.
DeleteQueue/DeleteQueuev2.go
This example deletes an Amazon SQS queue.
go run DeleteQueuev2.go -q QUEUE-NAME
- QUEUE-NAME is the name of the queue to delete.
The unit test accepts a similar value in config.json.
GetQueueURL/GetQueueURLv2.go
This example gets the URL of an Amazon SQS queue.
go run GetQueueURLv2.go -q QUEUE-NAME
- QUEUE-NAME is the name of the queue for which the URL is retrieved.
The unit test accepts a similar value in config.json.
ListQueues/ListQueuesv2.go
This example retrieves a list of your Amazon SQS queues.
go run ListQueuesv2.go
ReceiveLPMessage/ReceiveLPMessagev2.go
This example gets the most recent message from a long-polling Amazon SQS queue.
go run ReceiveLPMessagev2.go -q QUEUE-NAME
- QUEUE-NAME is the name of the queue from which the message is retrieved.
The unit test accepts a similar value in config.json.
ReceiveMessage/ReceiveMessagev2.go
This example gets the most recent message from an Amazon SQS queue.
go run ReceiveMessagev2.go -q QUEUE-NAME
- QUEUE-NAME is the name of the queue from which the message is retrieved.
The unit test accepts a similar value in config.json.
SendMessage/SendMessagev2.go
This example sends a message to an Amazon SQS queue.
go run SendMessagev2.go -q QUEUE-NAME
- QUEUE-NAME is the name of the queue to which the message is sent.
The unit test accepts a similar value in config.json.
Notes
- We recommend that you grant this code least privilege,
or at most the minimum permissions required to perform the task.
For more information, see
Grant Least Privilege
in the AWS Identity and Access Management User Guide.
- This code has not been tested in all AWS Regions.
Some AWS services are available only in specific
Regions.
- Running this code might result in charges to your AWS account.
Running the unit tests
Unit tests should delete any resources they create.
However, they might result in charges to your
AWS account.
To run a unit test, enter:
go test
You should see something like the following,
where PATH is the path to the folder containing the Go files:
PASS
ok PATH 6.593s
If you want to see any log messages, enter:
go test -v
You should see some additional log messages.
The last two lines should be similar to the previous output shown.
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0