Using Amazon SQS Queues - AWS SDK for Go (version 1)

We announced the upcoming end-of-support for AWS SDK for Go V1. We recommend that you migrate to AWS SDK for Go V2. For dates, additional details, and information on how to migrate, please refer to the linked announcement.

Using Amazon SQS Queues

These AWS SDK for Go examples show you how to:

  • List Amazon SQS queues

  • Create Amazon SQS queues

  • Get Amazon SQS queue URLs

  • Delete Amazon SQS queues

You can download complete versions of these example files from the aws-doc-sdk-examples repository on GitHub.

Scenario

These examples demonstrate how to work with Amazon SQS queues.

The code uses these methods of the Amazon SQS client class:

Prerequisites

  • You have set up and configured the AWS SDK for Go.

  • You are familiar with using Amazon SQS. To learn more, see How Queues Work in the Amazon SQS Developer Guide.

List Queues

Create a new Go file named ListQueues.go. You must import the relevant Go and AWS SDK for Go packages by adding the following lines.

import ( "fmt" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/sqs" )

Initialize a session that the SDK uses to load credentials from the shared credentials file, ~/.aws/credentials and the default region from ~/.aws/config.

sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, }))

Create a service client and call ListQueues, passing in nil to return all queues.

svc := sqs.New(sess) result, err := svc.ListQueues(nil)

Loop through the queue URLs to print them.

for i, url := range result.QueueUrls { fmt.Printf("%d: %s\n", i, *url) }

See the complete example on GitHub.

Create a Queue

Create a new Go file named CreateQueue.go. You must import the relevant Go and AWS SDK for Go packages by adding the following lines.

import ( "flag" "fmt" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/sqs" )

Get the queue name from the command line.

queue := flag.String("q", "", "The name of the queue") flag.Parse() if *queue == "" { fmt.Println("You must supply a queue name (-q QUEUE") return }

Initialize a session that the SDK uses to load credentials from the shared credentials file, ~/.aws/credentials and the default region from ~/.aws/config.

sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, }))

Create a service client and call CreateQueue, passing in the queue name and queue attributes.

svc := sqs.New(sess) result, err := svc.CreateQueue(&sqs.CreateQueueInput{ QueueName: queue, Attributes: map[string]*string{ "DelaySeconds": aws.String("60"), "MessageRetentionPeriod": aws.String("86400"), }, })

Display the queue URL.

fmt.Println("URL: " + *result.QueueUrl)

See the complete example on GitHub.

Get the URL of a Queue

Create a new Go file named GetQueueUrl.go. You must import the relevant Go and AWS SDK for Go packages by adding the following lines.

import ( "flag" "fmt" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/sqs" )

Get the name of the queue from the command line.

queue := flag.String("q", "", "The name of the queue") flag.Parse() if *queue == "" { fmt.Println("You must supply a queue name (-q QUEUE") return }

Initialize a session that the SDK uses to load credentials from the shared credentials file, ~/.aws/credentials and the default region from ~/.aws/config.

sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, }))

Create a service client and call GetQueueUrl, passing in the queue name.

svc := sqs.New(sess) result, err := svc.GetQueueUrl(&sqs.GetQueueUrlInput{ QueueName: queue, })

Display the URL of the queue.

fmt.Println("URL: " + *result.QueueUrl)

See the complete example on GitHub.

Delete a Queue

Create a new Go file named DeleteQueue.go. You must import the relevant Go and AWS SDK for Go packages by adding the following lines.

import ( "flag" "fmt" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/sqs" )

Get the name of the queue from the command line.

queue := flag.String("q", "", "The name of the queue") flag.Parse() if *queue == "" { fmt.Println("You must supply a queue name (-q QUEUE") return }

Initialize a session that the SDK uses to load credentials from the shared credentials file, ~/.aws/credentials and the default region from ~/.aws/config.

sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, }))

Create a service client and all DeleteQueue passing in the queue name.

svc := sqs.New(sess) result, err := svc.GetQueueUrl(&sqs.GetQueueUrlInput{ QueueName: queueName, })

See the complete example on GitHub.