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
Install Go.
Go 1.6 and later are supported. To obtain a Go installation package, visit the official Go website.
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 gopathcommand.Run the following command to install Alibaba Cloud SDK for Go:
go get -u github.com/aliyun/alibaba-cloud-sdk-go/sdkFor more information, visitalibaba-cloud-sdk-go.
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 |
Initiate a request
For a list of IoT Platform cloud APIs, see API list. For descriptions of the request parameters in `request` and the response parameters in `response`, see the corresponding API reference.
This topic uses the Pub operation as an example to show how to publish a message to a topic. For information about the request parameters, see Pub.
In the following code, ${iotInstanceId} represents the instance ID. You can find the ID of the current instance on the Instance Overview page in the IoT Platform console.
If an ID exists, you must pass this ID. Otherwise, the API call fails.
If the Instance Details page or the ID does not exist, you do not need to pass an ID. You can delete the request code related to `IotInstanceId` or pass an empty value `""`. Otherwise, the API call fails.
For more information about instances, see Instance overview. For information about how to purchase an instance, see Purchase an Enterprise instance. For frequently asked questions, 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 go to the IoT Platform Cloud SDK Example Center to view or download sample code for API calls. The sample code provides examples for the Java, Python, PHP, .NET, and Go SDKs.
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.