英文:
Create GO application that Reads from SQS without terminating
问题
我是Go的新手,绝对不是一个有经验的开发者,所以请随意批评。
我正在尝试创建一个Go应用程序,它将存在于一个容器中,并从AWS SQS中读取数据。目标是应用程序长时间运行并不断从队列中读取数据。
以下是我目前的示例代码。我最大的问题是,这种使用GOTO的方式是否不好,是否有更好的方法。
提前感谢你的帮助。
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"
)
const (
maxMessages = 1
)
func GetQueueURL(cfg aws.Config, queue string) (*sqs.GetQueueUrlOutput, error) {
sqsClient := sqs.NewFromConfig(cfg)
result, err := sqsClient.GetQueueUrl(context.TODO(), &sqs.GetQueueUrlInput{
QueueName: &queue,
})
if err != nil {
return nil, err
}
return result, nil
}
func GetMessages(cfg aws.Config, queueUrl string, maxMessages int32) (*sqs.ReceiveMessageOutput, error) {
sqsClient := sqs.NewFromConfig(cfg)
msgResult, err := sqsClient.ReceiveMessage(context.TODO(), &sqs.ReceiveMessageInput{
QueueUrl: &queueUrl,
MaxNumberOfMessages: maxMessages,
WaitTimeSeconds: 10,
})
if err != nil {
return nil, err
}
return msgResult, nil
}
func main() {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatal("error")
}
queueName := "queue"
res, err := GetQueueURL(cfg, queueName)
if err != nil {
fmt.Printf("Got an error receiving url: %v", err)
}
FINDMESSAGE:
msgRes, err := GetMessages(cfg, *res.QueueUrl, maxMessages)
if err != nil {
fmt.Printf("Got an error while trying to retrieve messages: %v", err)
}
if len(msgRes.Messages) != 0 {
fmt.Println("Message Body: " + *msgRes.Messages[0].Body)
fmt.Println("Message Handle: " + *msgRes.Messages[0].ReceiptHandle)
}
fmt.Println("No Messages")
goto FINDMESSAGE
}
英文:
I am new to Go and I am not an experienced dev by any means so please feel free to bash away.
I am attempting to create a GO application that will live in a container and read from AWS SQS. The goal would be the app is long living and constantly reads from the Queue.
This is the code I have so far as an example. My biggest question is if this is a bad use of GOTO and is there better way.
Thank you in advance
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"
)
const (
maxMessages = 1
)
func GetQueueURL(cfg aws.Config, queue string) (*sqs.GetQueueUrlOutput, error) {
sqsClient := sqs.NewFromConfig(cfg)
result, err := sqsClient.GetQueueUrl(context.TODO(), &sqs.GetQueueUrlInput{
QueueName: &queue,
})
if err != nil {
return nil, err
}
return result, nil
}
func GetMessages(cfg aws.Config, queueUrl string, maxMessages int32) (*sqs.ReceiveMessageOutput, error) {
sqsClient := sqs.NewFromConfig(cfg)
msgResult, err := sqsClient.ReceiveMessage(context.TODO(), &sqs.ReceiveMessageInput{
QueueUrl: &queueUrl,
MaxNumberOfMessages: maxMessages,
WaitTimeSeconds: 10,
})
if err != nil {
return nil, err
}
return msgResult, nil
}
func main() {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatal("error")
}
queueName := "queue"
res, err := GetQueueURL(cfg, queueName)
if err != nil {
fmt.Printf("Got an error receiving url: %v", err)
}
FINDMESSAGE:
msgRes, err := GetMessages(cfg, *res.QueueUrl, maxMessages)
if err != nil {
fmt.Printf("Got an error while trying to retrieve messages: %v", err)
}
if len(msgRes.Messages) != 0 {
fmt.Println("Message Body: " + *msgRes.Messages[0].Body)
fmt.Println("Message Handle: " + *msgRes.Messages[0].ReceiptHandle)
}
fmt.Println("No Messages")
goto FINDMESSAGE
}
</details>
# 答案1
**得分**: 0
任何使用`goto`都是不好的,除非你能解释为什么要使用`goto`。你的例子是一个for循环:
```go
for {
msgRes, err := GetMessages(cfg, *res.QueueUrl, maxMessages)
if err != nil {
fmt.Printf("在尝试检索消息时出错:%v", err)
}
if len(msgRes.Messages) != 0 {
fmt.Println("消息正文:" + *msgRes.Messages[0].Body)
fmt.Println("消息句柄:" + *msgRes.Messages[0].ReceiptHandle)
}
fmt.Println("没有消息")
}
请注意,这段代码中没有使用goto
语句。如果你有关于goto
语句的问题,请提供更多上下文信息。
英文:
> My biggest question is if this is a bad use of GOTO and is there better way.
any use of goto
is a bad use of goto
, unless you can express why you use a goto. Your example is a for loop:
for {
msgRes, err := GetMessages(cfg, *res.QueueUrl, maxMessages)
if err != nil {
fmt.Printf("Got an error while trying to retrieve messages: %v", err)
}
if len(msgRes.Messages) != 0 {
fmt.Println("Message Body: " + *msgRes.Messages[0].Body)
fmt.Println("Message Handle: " + *msgRes.Messages[0].ReceiptHandle)
}
fmt.Println("No Messages")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论