Go SDK

更新时间:
复制 MD 格式

This topic provides release notes, usage instructions, and sample code for the SDK for Go.

Usage

  1. Download and decompress the latest version of the SDK for Go, then navigate to the aliyun-mns-go-sdk root directory.

  2. Go to the example directory. In the queue_example.go or topic_example.go file, replace the placeholder endpoint with your instance endpoint. Then, set the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

    You can find your endpoint in the Endpoint section on the Queue Details or Topic Details page in the console. The instance endpoint is located in the Endpoint section on the Queue Details page in the Simple Message Queue console. Both public and internal endpoints are provided in this section.

Sample code

Queue model

This example creates a queue, sends a message, receives and deletes the message, and then deletes the 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
	}
}

Topic model

This example creates a queue and a topic, subscribes to the topic, and publishes a message.

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
}

Release notes

Version 1.0.11

Release date

Description

Download

2025-03-20

Rolled back the golang.org/x/net dependency from v0.36.0 to v0.33.0.

Download SDK

Version 1.0.10

Release date

Description

Download

2025-03-17

Fixed an issue where the region check failed when creating endpoint resources suffixed with -control.

Download SDK

Version 1.0.9

Release date

Description

Download

2025-03-11

Fixed issues#26, an error caused by the missing StsTokenCredential component in the credential package.

Download SDK

Version 1.0.8

Release date

Description

Download

2025-02-06

Added support for configuring the logEnable parameter during queue creation and property updates.

Download SDK

Version 1.0.7

Release date

Description

Download

2025-01-23

  • Added Base64 encoding and decoding examples to queue_example.go.

  • Added support for dynamic credentials.

Download SDK

Version 1.0.6

Release date

Description

Download

2024-11-13

  • Added topic_example.go, an example of an HTTP endpoint subscription in the topic-based messaging model.

  • Added http_authorization.go, an example of HTTP signature verification.

  • Removed the check on message body size to allow larger messages.

Download SDK

Version 1.0.5

Release date

Description

Download

2024-08-19

Updated the minimum Go version declared in the go.mod file to fix a build failure.

Download SDK

Version 1.0.4

Release date

Description

Download

2024-07-17

  • Added support for customizing the maxConnsPerHost value on the client.

  • Included the SDK version and OS information in the client details.

  • Added a client initialization method that supports reading configuration from environment variables.

Download SDK

Version 1.0.3

Release date

Description

Download

2024-05-21

Added support for custom HTTP Transport configurations.

Download SDK

Version 1.0.2

Release date

Description

Download

2021-03-05

Added support for the OpenService API.

Download SDK

Version 1.0.1

Release date

Description

Download

2021-01-15

  • Added support for setting timeouts.

  • Included the Request ID in responses.

Download SDK

Version 1.0.0

Release date

Description

Download

2019-04-30

  • Added operations for the queue-based messaging model:

    • Create, modify, retrieve, and delete queues.

    • Send, peek, receive, and delete messages, and change a message's visibility timeout.

  • Added operations for the topic-based messaging model:

    • Create, modify, and delete topics.

    • Create and delete subscriptions.

    • Publish messages to a topic.

Download SDK