AWS SDK for Go
Developer Guide

Managing Visibility Timeout in Amazon SQS Queues

This AWS SDK for Go example shows you how to:

  • Change visibility timeout with Amazon SQS queues

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


This example manages visibility timeout with Amazon SQS queues. It uses these methods of the Amazon SQS client class:


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

  • You are familiar with using Amazon SQS visibility timeout. To learn more, see Visibility Timeout in the Amazon SQS Developer Guide.

Change the Visibility Timeout

Create a new Go file named sqs_changingvisibility.go.

You must import the relevant Go and AWS SDK for Go packages by adding the following lines.

package main import ( "fmt" "" "" "" )

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

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

Get a message from the queue. Call ReceiveMessage. Pass in the URL of the queue to return details of the next message in the queue. Print any errors, or a message if no message was received.

qURL := "QueueURL" result, err := svc.ReceiveMessage(&sqs.ReceiveMessageInput{ AttributeNames: []*string{ aws.String(sqs.MessageSystemAttributeNameSentTimestamp), }, MaxNumberOfMessages: aws.Int64(1), MessageAttributeNames: []*string{ aws.String(sqs.QueueAttributeNameAll), }, QueueUrl: &qURL, }) if err != nil { fmt.Println("Error", err) return } // Check if we have any messages if len(result.Messages) == 0 { fmt.Println("Received no messages") return }

If a message was returned, use its receipt handle to set the timeout to 30 seconds.

duration := int64(30) resultVisibility, err := svc.ChangeMessageVisibility(&sqs.ChangeMessageVisibilityInput{ ReceiptHandle: result.Messages[0].ReceiptHandle, QueueUrl: &qURL, VisibilityTimeout: &duration, }) if err != nil { fmt.Println("Visibility Error", err) return } fmt.Println("Time Changed", resultVisibility) }