All Products
Search
Document Center

IoT Platform:Use IoT Platform SDK for Go

最終更新日:Oct 22, 2023

IoT Platform provides an SDK for Go. This topic describes how to install and configure IoT Platform SDK for Go. This topic also provides sample code on how to use the SDK to call 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, visitalibaba-cloud-sdk-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/sdk"
    "os"
)
func main() {
    accessKeyId := os.Getenv("ACCESS_KEY_ID")
    accessKeySecret := os.Getenv("ACCESS_KEY_SECRET")
    client, err := sdk.NewClientWithAccessKey("<your regionId>", accessKeyId, accessKeySecret)
    if err != nil {
        // Handle exceptions
        panic(err)
    }
}

Parameter

Description

regionId

The region ID of IoT Platform. For example, the ID of the China (Shanghai) region is cn-shanghai. For more information, see Supported regions.

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 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.

Important

In the following sample code, ${iotInstanceId} specifies the ID of an instance. You can view the ID of the instance on the Overview page in the IoT Platform console.

  • If your instance has an ID, you must specify the ID for this parameter. Otherwise, the call fails.

  • If no Overview page or ID is generated for your instance, you do not need to configure this parameter. You must delete the request code that is related to the IotInstanceId parameter or specify an empty string ("") for the parameter. Otherwise, the call fails.

For more information about IoT Platform instances, see Overview. For information about how to purchase an instance, see Purchase Enterprise Edition instances. For more information, see FAQ about IoT Platform instances.

request := iot.CreatePubRequest()
request.AcceptFormat = "json"
request.IotInstanceId = "<your iotInstanceId>"
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

You can view or download the sample code of API operations in IoT Platform SDK Sample Center. Sample code of SDKs for Java, Python, PHP, .NET, and Go is provided.

Alibaba Cloud OpenAPI Explorer provides online debugging tools for API operations. On the API Debugging page, you can search for API operations, call API operations, and generate sample code for API operations of different SDKs. On the right side of the page, you can view the sample code of an SDK on the Sample Code tab. On the Debugging Result tab, you can view the actual request URL and response in the JSON format.