IoT Platform provides developers with an SDK for Go. This article describes how to install and configure IoT Platform SDK for Go. This article also describes how to use the SDK to call the API operations of IoT Platform.

Install the SDK

  1. Install Go.

    Go 1.6 and later are supported. To obtain a Go installation package, visit the official Go website.

  2. After Go is installed, create a system variable named GOPATH and set the value to your code directory.
    To obtain more information about the GOPATH variable, run the go help gopath command.
  3. Run the following command to install Alibaba Cloud SDK for Go:
    go get -u github.com/aliyun/alibaba-cloud-sdk-go/sdk

    For more information, see Alibaba Cloud SDK for Go.

  4. Run the following command to import the file that is related to IoT Platform SDK for Go to a Go file:
    import "github.com/aliyun/alibaba-cloud-sdk-go/services/iot"

Initialize the SDK

package main

import "github.com/aliyun/alibaba-cloud-sdk-go/services/iot"

func main() {

    client, err := sdk.NewClientWithAccessKey("<your regionId>", "<your accessKey>", 
                                              "<your accessSecret>")
    if err != nil {
        // Handle exceptions
        panic(err)
    }
}
Parameter Description
regionId The ID of the region where IoT Platform resides. For example, the ID of the China (Shanghai) region is cn-shanghai. For more information, see Regions and zones.
accessKey The AccessKey ID of your Alibaba Cloud account. You can go to the User Management console to create or view your AccessKey ID.
accessSecret The AccessKey secret that is used in pair with the AccessKey ID. You can go to the User Management console to create or view your AccessKey secret.

Initiate a request

For information about the API operations of IoT Platform, see List of operations by function.

The following example shows how to call the Pub operation to publish messages to a topic.

request := iot.CreatePubRequest()
request.AcceptFormat = "json"
request.ProductKey = "<your productKey>"
var builder strings.Builder
builder.WriteString("/")
builder.WriteString(request.ProductKey)
builder.WriteString("/")
builder.WriteString("<your deviceName>")
builder.WriteString("/user/update")
request.TopicFullName = builder.String()
request.MessageContent = base64.StdEncoding.EncodeToString([]byte("hello world"))
request.Qos = "0"
response, err := client.Pub(request)
if err != nil {
    fmt.Print(err.Error())
}
fmt.Printf("response is %#v\n", response)

Appendix: Sample code

Download the SDK demo. The demo includes the sample code for Java, Python, PHP, .NET, and Go.

Alibaba Cloud provides OpenAPI Explorer to simplify API usage. You can use OpenAPI Explorer to search for API operations, call API operations, and dynamically generate the sample code of API operations for different SDKs. On the right side of the page, you can view the sample code of an SDK on the Example Code tab. On the Debugging Result tab, you can view the actual request URL and response in the JSON format.