Menu
AWS SDK for Go
Developer Guide

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 sqs_listqueues.go. You must import the relevant Go and AWS SDK for Go packages by adding the following lines.

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

Initialize a session that the SDK will use to load credentials from the shared credentials file, ~/.aws/credentials.

Copy
func main() { sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, ) // Create a SQS service client. svc := sqs.New(sess)

Call ListQueues passing in nil to return all queues. Print any errors or a success message and loop through the queue URLs to print them.

Copy
result, err := svc.ListQueues(nil) if err != nil { fmt.Println("Error", err) return } fmt.Println("Success") // As these are pointers, printing them out directly would not be useful. for i, urls := range result.QueueUrls { // Avoid dereferencing a nil pointer. if urls == nil { continue } fmt.Printf("%d: %s\n", i, *urls) } }

Create Queues

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

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

Initialize a session that the SDK will use to load credentials from the shared credentials file, ~/.aws/credentials.

Copy
func main() { sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, ) // Create a SQS service client. svc := sqs.New(sess)

Call CreateQueue passing in the new queue name and queue attributes. Print any errors or a success message.

Copy
result, err := svc.CreateQueue(&sqs.CreateQueueInput{ QueueName: aws.String("SQS_QUEUE_NAME"), Attributes: map[string]*string{ "DelaySeconds": aws.String("60"), "MessageRetentionPeriod": aws.String("86400"), }, }) if err != nil { fmt.Println("Error", err) return } fmt.Println("Success", *result.QueueUrl) }

Get a Queue URL

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

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

Initialize a session that the SDK will use to load credentials from the shared credentials file, ~/.aws/credentials.

Copy
func main() { sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, ) // Create a SQS service client. svc := sqs.New(sess)

Call GetQueueUrl passing in the queue name. Print any errors or a success message.

Copy
result, err := svc.GetQueueUrl(&sqs.GetQueueUrlInput{ QueueName: aws.String("SQS_QUEUE_NAME"), }) if err != nil { fmt.Println("Error", err) return } fmt.Println("Success", *result.QueueUrl) }

Delete a Queue

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

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

Initialize a session that the SDK will use to load credentials from the shared credentials file, ~/.aws/credentials.

Copy
func main() { sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, ) // Create a SQS service client. svc := sqs.New(sess)

Call DeleteQueue passing in the queue name. Print any errors or a success message.

Copy
result, err := svc.DeleteQueue(&sqs.DeleteQueueInput{ QueueUrl: aws.String("SQS_QUEUE_URL"), }) if err != nil { fmt.Println("Error", err) return } fmt.Println("Success", result) }