Skip to content

Instantly share code, notes, and snippets.

@wbrady
Last active August 29, 2015 14:07
Show Gist options
  • Save wbrady/15082e7e642a402d8129 to your computer and use it in GitHub Desktop.
Save wbrady/15082e7e642a402d8129 to your computer and use it in GitHub Desktop.
Amazon SQS Example in Go
package main
import (
sqs "github.com/crowdmob/goamz/sqs"
"log"
"fmt"
"flag"
)
var AWSAccessKey string
var AWSSecretKey string
func init() {
flag.StringVar(&AWSAccessKey, "access_key", "AWS ACCESS KEY", "AWS access key")
flag.StringVar(&AWSSecretKey, "secret_key", "AWS SECRET KEY", "AWS secret key")
flag.Parse()
}
func ConnectToQueue(name string) (queue sqs.Queue) {
sqsInstance, err := sqs.NewFrom(AWSAccessKey, AWSSecretKey, "us-east-1")
if err != nil {
log.Fatalln("Failed to authenticate to SQS: ", err)
}
queue = sqs.Queue{
SQS: sqsInstance,
Url: fmt.Sprintf("https://sqs.us-east-1.amazonaws.com/543524483915/%s", name),
}
return
}

Amazon SQS Example in Go

Here is an example sending and receiving messages. The first commands sends two messages. The next receive command receives both messages (but it could have just gotten one of them) and deletes them. The next receive command doesn't get any because there aren't any left in the queue.

The queue name ("test" in my case) and AWS region is hard-coded in this example. I set the queue up manually in AWS beforehand.

http://godoc.org/github.com/crowdmob/goamz/sqs

> go run connect.go send.go -access_key="MY_ACCESS_KEY" -secret_key="MY_SECRET_KEY"
2014/10/11 22:57:00 Sending test message...
2014/10/11 22:57:00 Message ID:  b226dbf4-6cd3-459f-a086-5c95e970308a
2014/10/11 22:57:00
2014/10/11 22:57:00 Sending test message with attributes...
2014/10/11 22:57:01 Message ID:  17080a5d-bc3b-4fc5-bf27-4ea33c7d90d2

> go run connect.go receive.go -access_key="MY_ACCESS_KEY" -secret_key="MY_SECRET_KEY"
2014/10/11 22:57:02 Receiving messages...
2014/10/11 22:57:02 Found 2 messages
2014/10/11 22:57:02 Message 1:
2014/10/11 22:57:02 ID: b226dbf4-6cd3-459f-a086-5c95e970308a
2014/10/11 22:57:02 Body: test message 1
2014/10/11 22:57:02 SenderId: AIDAJLP7HF2SYE44UJFC4
2014/10/11 22:57:02 ApproximateFirstReceiveTimestamp: 1413082622932
2014/10/11 22:57:02 ApproximateReceiveCount: 1
2014/10/11 22:57:02 SentTimestamp: 1413082620924
2014/10/11 22:57:02
2014/10/11 22:57:02 Message 2:
2014/10/11 22:57:02 ID: 17080a5d-bc3b-4fc5-bf27-4ea33c7d90d2
2014/10/11 22:57:02 Body: test message with attributes 1
2014/10/11 22:57:02 SenderId: AIDAJLP7HF2SYE44UJFC4
2014/10/11 22:57:02 ApproximateFirstReceiveTimestamp: 1413082622940
2014/10/11 22:57:02 ApproximateReceiveCount: 1
2014/10/11 22:57:02 SentTimestamp: 1413082620962
2014/10/11 22:57:02 (String) random_attribute_1: "value 1"
2014/10/11 22:57:02 (String) random_attribute_2: "value 2"
2014/10/11 22:57:02
2014/10/11 22:57:02 Deleting messages...

> go run connect.go receive.go -access_key="MY_ACCESS_KEY" -secret_key="MY_SECRET_KEY"
2014/10/11 22:57:14 Receiving messages...
2014/10/11 22:57:15 Found 0 messages
package main
import (
"log"
)
func main() {
testQueue := ConnectToQueue("test")
log.Print("Receiving messages...")
resp, err := testQueue.ReceiveMessage(10)
if err != nil {
log.Fatalln("Failed to send test message: ", err)
}
log.Print("Found ", len(resp.Messages), " messages")
for index, message := range resp.Messages {
log.Print("Message ", index+1, ":")
log.Print("ID: ", message.MessageId)
log.Print("Body: ", message.Body)
for _, attribute := range message.Attribute {
log.Print(attribute.Name, ": ", attribute.Value)
}
for _, messageAttribute := range message.MessageAttribute {
log.Print("(", messageAttribute.Value.DataType, ") ", messageAttribute.Name, ": \"", messageAttribute.Value.StringValue, "\"")
}
log.Println()
}
if len(resp.Messages) > 0 {
log.Println("Deleting messages...")
_, err := testQueue.DeleteMessageBatch(resp.Messages)
if err != nil {
log.Fatalln("Failed to delete messages: ", err)
}
}
}
package main
import (
"log"
)
func main() {
testQueue := ConnectToQueue("test")
log.Println("Sending test message...")
resp, err := testQueue.SendMessage("test message 1")
if err != nil {
log.Fatalln("Failed to send test message: ", err)
}
log.Print("Message ID: ", resp.Id)
log.Println()
log.Print("Sending test message with attributes...")
resp, err = testQueue.SendMessageWithAttributes("test message with attributes 1", map[string]string{
"random_attribute_1": "value 1",
"random_attribute_2": "value 2",
})
if err != nil {
log.Fatalln("Failed to send test message with attributes: ", err)
}
log.Print("Message ID: ", resp.Id)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment