Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
  • Blog
  • Pricing
  • Documentation
Yandex project
© 2025 Yandex.Cloud LLC
Yandex Message Queue
    • Quick start
    • Supported tools
      • Python
      • Node.js
      • PHP
      • Celery
      • JMS
      • Laravel
      • Symfony
      • Terraform
      • Golang
  • Access management
  • Pricing policy
  • Terraform reference
  • Monitoring metrics
  • Public materials
  • FAQ

In this article:

  • Installing
  • Getting started
  • Example
  1. Getting started
  2. Code snippets
  3. Golang

Example of using Yandex Message Queue in Golang

Written by
Yandex Cloud
Improved by
Alex
Updated at March 28, 2025
  • Installing
  • Getting started
  • Example

With the AWS SDK for Golang, you can manage Message Queue message queues and send/receive messages.

InstallingInstalling

Install the AWS SDK for Golang by following the instructions on the official website.

Getting startedGetting started

  1. Create a service account.
  2. Assign the editor role to the service account.
  3. Create a static access key.

Set the environment variables:

export AWS_ACCESS_KEY_ID="<access_key_ID>"
export AWS_SECRET_ACCESS_KEY="<secret_key>"

ExampleExample

In this example:

  1. A connection with Message Queue is established.
  2. A message queue named mq_example_golang_sdk is created.
  3. A message with the text test message is sent to the queue.
  4. The message is read from the queue and displayed in the terminal.
  5. The message queue is deleted.
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/sqs"
)

func main() {
	ctx := context.Background()

	customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
		return aws.Endpoint{
			URL:           "https://message-queue.api.cloud.yandex.net",
			SigningRegion: "ru-central1",
		}, nil
	})

	cfg, err := config.LoadDefaultConfig(
		ctx,
		config.WithEndpointResolverWithOptions(customResolver),
	)
	if err != nil {
		log.Fatalln(err)
	}

	client := sqs.NewFromConfig(cfg)

	queueName := "mq_example_golang_sdk"

	queue, err := client.CreateQueue(ctx, &sqs.CreateQueueInput{
		QueueName: &queueName,
	})
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println("Queue created, URL: " + *queue.QueueUrl)

	msg := "test message"

	send, err := client.SendMessage(ctx, &sqs.SendMessageInput{
		QueueUrl:    queue.QueueUrl,
		MessageBody: &msg,
	})
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println("Message sent, ID: " + *send.MessageId)

	received, err := client.ReceiveMessage(ctx, &sqs.ReceiveMessageInput{
		QueueUrl: queue.QueueUrl,
	})
	if err != nil {
		log.Fatalln(err)
	}

	for _, v := range received.Messages {
		fmt.Printf("Message received\nID: %s\nBody: %s\n", *v.MessageId, *v.Body)

		if _, err := client.DeleteMessage(
			ctx,
			&sqs.DeleteMessageInput{
				QueueUrl:      queue.QueueUrl,
				ReceiptHandle: v.ReceiptHandle,
			},
		); err != nil {
			log.Fatalln(err)
		}
	}

	if _, err := client.DeleteQueue(ctx, &sqs.DeleteQueueInput{
		QueueUrl: queue.QueueUrl,
	}); err != nil {
		log.Fatalln(err)
	}

	fmt.Println("Queue deleted")
}

Was the article helpful?

Previous
Terraform
Next
Overview
Yandex project
© 2025 Yandex.Cloud LLC