The Simple Message Queue (formerly MNS) SDK for Go provides queue-based and topic-based messaging capabilities, including queue and topic management, message sending, receiving, and deletion, and topic subscriptions.
Prerequisites
Before you begin, make sure that you have:
Go 1.20 or later
An Alibaba Cloud account with MNS activated
An AccessKey ID and AccessKey secret. Set them as environment variables:
export ALIBABA_CLOUD_ACCESS_KEY_ID=<your-access-key-id> export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<your-access-key-secret>Your MNS endpoint. To find the endpoint, go to the Queue Details or Topic Details page in the console and check Endpoint on the Basic Information tab.

Install the SDK
Run the following command to install the SDK:
go get github.com/aliyun/aliyun-mns-go-sdkSample code
Queue-based messaging model
The following example creates a queue, sends messages, receives messages, deletes messages, and deletes a queue.
package main
import (
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"time"
"github.com/aliyun/aliyun-mns-go-sdk"
"github.com/gogap/logs"
)
func main() {
go func() {
log.Println(http.ListenAndServe("localhost:8080", nil))
}()
// Replace with your own endpoint.
endpoint := "http://xxx.mns.cn-hangzhou.aliyuncs.com"
client := ali_mns.NewClient(endpoint)
msg := ali_mns.MessageSendRequest{
MessageBody: "hello <\"aliyun-mns-go-sdk\">",
DelaySeconds: 0,
Priority: 8}
queueManager := ali_mns.NewMNSQueueManager(client)
queueName := "test-queue"
err := queueManager.CreateQueue(queueName, 0, 65536, 345600, 30, 0, 3)
time.Sleep(time.Duration(2) * time.Second)
if err != nil && !ali_mns.ERR_MNS_QUEUE_ALREADY_EXIST_AND_HAVE_SAME_ATTR.IsEqual(err) {
fmt.Println(err)
return
}
queue := ali_mns.NewMNSQueue(queueName, client)
for i := 1; i < 10000; i++ {
ret, err := queue.SendMessage(msg)
go func() {
fmt.Println(queue.QPSMonitor().QPS())
}()
if err != nil {
fmt.Println(err)
} else {
logs.Pretty("response: ", ret)
}
endChan := make(chan int)
respChan := make(chan ali_mns.MessageReceiveResponse)
errChan := make(chan error)
go func() {
select {
case resp := <-respChan:
{
logs.Pretty("response: ", resp)
logs.Debug("change the visibility: ", resp.ReceiptHandle)
if ret, e := queue.ChangeMessageVisibility(resp.ReceiptHandle, 5); e != nil {
fmt.Println(e)
} else {
logs.Pretty("visibility changed", ret)
logs.Debug("delete it now: ", ret.ReceiptHandle)
if e := queue.DeleteMessage(ret.ReceiptHandle); e != nil {
fmt.Println(e)
}
endChan <- 1
}
}
case err := <-errChan:
{
fmt.Println(err)
endChan <- 1
}
}
}()
queue.ReceiveMessage(respChan, errChan, 30)
<-endChan
}
}This example demonstrates the following operations:
Create a client --
ali_mns.NewClient(endpoint)initializes the MNS client with your endpoint.Create a queue --
CreateQueueaccepts the queue name, delay seconds (0), maximum message size (65536 bytes), message retention period (345600 seconds), visibility timeout period (30 seconds), polling wait seconds (0), and slices (3).Send a message --
SendMessagesends aMessageSendRequestwith the message content, delay, and priority.Receive a message --
ReceiveMessageuses channels for asynchronous message reception with a 30-second wait.Change visibility timeout period --
ChangeMessageVisibilityextends the visibility timeout period using the receipt handle and returns a new receipt handle.Delete a message --
DeleteMessageremoves a message from the queue using its receipt handle.
Topic-based messaging model
The following example creates a queue, creates a topic, subscribes to a topic, and publishes messages.
package main
import (
"fmt"
"time"
"github.com/aliyun/aliyun-mns-go-sdk"
"github.com/gogap/logs"
)
func main() {
// Replace with your own endpoint.
endpoint := "http://xxx.mns.cn-hangzhou.aliyuncs.com"
queueName := "test-queue"
topicName := "test-topic"
queueSubName := "test-sub-queue"
httpSubName := "test-sub-http"
client := ali_mns.NewClient(endpoint)
// 1. create a queue for receiving pushed messages
queueManager := ali_mns.NewMNSQueueManager(client)
err := queueManager.CreateSimpleQueue(queueName)
if err != nil && !ali_mns.ERR_MNS_QUEUE_ALREADY_EXIST_AND_HAVE_SAME_ATTR.IsEqual(err) {
fmt.Println(err)
return
}
// 2. create the topic
topicManager := ali_mns.NewMNSTopicManager(client)
// topicManager.DeleteTopic("testTopic")
err = topicManager.CreateSimpleTopic(topicName)
if err != nil && !ali_mns.ERR_MNS_TOPIC_ALREADY_EXIST_AND_HAVE_SAME_ATTR.IsEqual(err) {
fmt.Println(err)
return
}
topic := ali_mns.NewMNSTopic(topicName, client)
// 3. subscribe to topic, the endpoint is queue
queueSub := ali_mns.MessageSubsribeRequest{
Endpoint: topic.GenerateQueueEndpoint(queueName),
NotifyContentFormat: ali_mns.SIMPLIFIED,
}
// 4. subscribe to topic, the endpoint is HTTP(S)
httpSub := ali_mns.MessageSubsribeRequest{
Endpoint: "http://www.baidu.com",
NotifyContentFormat: ali_mns.SIMPLIFIED,
}
err = topic.Subscribe(queueSubName, queueSub)
if err != nil && !ali_mns.ERR_MNS_SUBSCRIPTION_ALREADY_EXIST_AND_HAVE_SAME_ATTR.IsEqual(err) {
fmt.Println(err)
return
}
err = topic.Subscribe(httpSubName, httpSub)
if err != nil && !ali_mns.ERR_MNS_SUBSCRIPTION_ALREADY_EXIST_AND_HAVE_SAME_ATTR.IsEqual(err) {
fmt.Println(err)
return
}
/*
sub = ali_mns.MessageSubsribeRequest{
Endpoint: topic.GenerateMailEndpoint("a@b.com"),
NotifyContentFormat: ali_mns.SIMPLIFIED,
}
err = topic.Subscribe("SubscriptionNameB", sub)
if (err != nil && !ali_mns.ERR_MNS_SUBSCRIPTION_ALREADY_EXIST_AND_HAVE_SAME_ATTR.IsEqual(err)) {
fmt.Println(err)
return
}
*/
time.Sleep(time.Duration(2) * time.Second)
// 5. now publish message
msg := ali_mns.MessagePublishRequest{
MessageBody: "hello topic <\"aliyun-mns-go-sdk\">",
MessageAttributes: &ali_mns.MessageAttributes{
MailAttributes: &ali_mns.MailAttributes{
Subject: "AAA Chinese characters",
AccountName: "BBB",
},
},
}
_, err = topic.PublishMessage(msg)
if err != nil {
fmt.Println(err)
return
}
// 6. receive the message from queue
queue := ali_mns.NewMNSQueue(queueName, client)
endChan := make(chan int)
respChan := make(chan ali_mns.MessageReceiveResponse)
errChan := make(chan error)
go func() {
select {
case resp := <-respChan:
{
logs.Pretty("response: ", resp)
fmt.Println("change the visibility: ", resp.ReceiptHandle)
if ret, e := queue.ChangeMessageVisibility(resp.ReceiptHandle, 5); e != nil {
fmt.Println(e)
} else {
logs.Pretty("visibility changed", ret)
fmt.Println("delete it now: ", ret.ReceiptHandle)
if e := queue.DeleteMessage(ret.ReceiptHandle); e != nil {
fmt.Println(e)
}
endChan <- 1
}
}
case err := <-errChan:
{
fmt.Println(err)
endChan <- 1
}
}
}()
queue.ReceiveMessage(respChan, errChan, 30)
<-endChan
}This example demonstrates the following operations:
Create a queue --
CreateSimpleQueuecreates a queue to receive messages pushed from topic subscriptions.Create a topic --
CreateSimpleTopiccreates a topic for publishing messages.Subscribe with a queue endpoint --
topic.GenerateQueueEndpoint(queueName)generates the queue endpoint, andtopic.Subscriberegisters the subscription withSIMPLIFIEDnotification content format.Subscribe with an HTTP(S) endpoint -- Subscriptions can also push messages to an HTTP(S) URL.
Publish a message --
PublishMessagepublishes aMessagePublishRequestto the topic. The request can includeMessageAttributessuch asMailAttributeswithSubjectandAccountName.Receive the message -- Messages published to the topic are pushed to the subscription queue. Use
ReceiveMessageto consume them.
The commented-out code block shows how to create a mail endpoint subscription using topic.GenerateMailEndpoint("a@b.com").Release notes
| Version | Release date | Description | Download |
|---|---|---|---|
| 1.0.11 | 2025-03-20 | golang.org/x/net is rolled back from v0.36.0 to v0.33.0. | Download SDK |
| 1.0.10 | 2025-03-17 | Fixed: Region information check failed after creating endpoint resources suffixed with -control. | Download SDK |
| 1.0.9 | 2025-03-11 | Fixed the version error issues#26 caused by unavailable StsTokenCredential in confidential files. | Download SDK |
| 1.0.8 | 2025-02-06 | Added support for the logEnable parameter when creating queues or configuring properties. | Download SDK |
| 1.0.7 | 2025-01-23 | Added Base64 encoding and decoding in queue_example.go. Credentials can now be obtained dynamically. | Download SDK |
| 1.0.6 | 2024-11-13 | Added topic_example.go for HTTP endpoint subscription in the topic-based messaging model. Added http_authorization.go for HTTP signature verification. Removed message content size limits to allow larger messages. | Download SDK |
| 1.0.5 | 2024-08-19 | Updated the earliest Go version in go.mod to prevent creation failures. | Download SDK |
| 1.0.4 | 2024-07-17 | Added support for configuring maxConnsPerHost. Added version number and operating system information to the client. Provided a new client initialization method that reads configuration from environment variables. | Download SDK |
| 1.0.3 | 2024-05-21 | Added support for setting the transport attribute of the HTTP client. | Download SDK |
| 1.0.2 | 2021-03-05 | Added the OpenService operation. | Download SDK |
| 1.0.1 | 2021-01-15 | Added timeout settings. Request IDs are now included in responses. | Download SDK |
| 1.0.0 | 2019-04-30 | Initial release. Queue management operations: create, modify, query, and delete queues; send, view, receive, and delete messages; change the value of the NextVisibleTime parameter. Topic management operations: create, modify, and delete topics; create and delete subscriptions; send messages. | Download SDK |