Example of using Yandex Message Queue in Golang
With the AWS SDK for Golang
Installing
Install the AWS SDK for Golang by following the instructions
Getting started
- Create a service account.
- Assign the editor role to the service account.
- Create a static access key.
Set the environment variables:
export AWS_ACCESS_KEY_ID="<access_key_ID>"
export AWS_SECRET_ACCESS_KEY="<secret_key>"
Example
In this example:
- A connection with Message Queue is established.
- A message queue named
mq_example_golang_sdk
is created. - A message with the text
test message
is sent to the queue. - The message is read from the queue and displayed in the terminal.
- 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")
}