IoT Platform provides a Go SDK for developers. This topic describes how to install and configure the Go SDK and how to use the Go SDK to call cloud APIs.

Install the SDK

  1. Install the Go development environment.

    Go 1.6 and later versions are supported. Visit the Go official website to download the latest version.

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

    For more information about the Go SDK, see alibaba-cloud-sdk-go.

  4. Use the following code to import the IoT Go SDK file to the 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 region code of the Alibaba Cloud service. For example, China (Shanghai) 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 of your Alibaba Cloud account. You can go to the User Management console to view your AccessKey secret.

Initiate a request

The following example demonstrates 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)