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 files that are 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 more information about the API operations of IoT Platform, see List of operations by function. For more information about the request parameters and response parameters of each API operation, see the API documentation.

The following example shows how to call the Pub operation to publish a message to a topic. For more information about request parameters, see Pub.

request := iot.CreatePubRequest()
request.AcceptFormat = "json"
request.ProductKey = "<your productKey>" 
request.TopicFullName = fmt.Sprintf("/%s/%s/user/get", "<your productKey>", "<your deviceName>")
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 IoT Platform SDK sample code. The sample code for Java, Python, PHP,.NET, and Go is provided.

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 SDK sample code. OpenAPI Explorer dynamically generates the sample code of the operation 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.