Application Go SDK
IoTDA provides an application SDK in Go for developers. This topic describes how to install and configure the Go SDK and how to use it to call application-side APIs.
Obtaining and Installing the SDK
- Install the Go development environment.
Visit the Go website, and download and install the Go development environment.
The Go SDK can be used in Go 1.14 and later versions.
- Install the Huawei Cloud Go library.
go get github.com/huaweicloud/huaweicloud-sdk-go-v3
- Install dependencies.
go get github.com/json-iterator/go
Code Sample
The following code sample shows how to use the Go SDK to call API Querying the Device List.
- Create a credential.
- Create and initialize an IoTDAClient instance.
- Instantiate a request object.
- Call the API for querying the device list.
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" // For the standard or enterprise edition, use github.com/huaweicloud/huaweicloud-sdk-go-v3/core/region. // For the basic edition, use github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotda/v5/region. "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/region" //"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotda/v5/region" iotda "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotda/v5" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotda/v5/model" ) func main() { // There will be security risks if the AK/SK used for authentication is directly written into code. Encrypt the AK/SK in the configuration file or environment variables for storage; // In this example, the AK/SK stored in the environment variables are used. Configure the environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK in the local environment first. ak := os.Getenv("HUAWEICLOUD_SDK_AK") sk := os.Getenv("HUAWEICLOUD_SDK_SK") projectId := "<YOUR PROJECTID>" // The regionId and endpoint are used to create regions for the standard edition and enterprise edition. For the basic edition, delete these two lines. // region_id: If CN East-Shanghai1 is used, enter cn-east-3. If CN North-Beijing4 is used, enter cn-north-4. If CN South-Guangzhou is used, enter cn-south-1. regionId := "<YOUR REGION ID>" // endpoint: On the console, choose Overview and click Access Addresses to view the HTTPS application access address. endpoint := "<YOUR ENDPOINT>" // Create a credential. auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). WithProjectId(projectId). // WithDerivedPredicate is used for the standard or enterprise edition. For the basic edition, delete the line. WithDerivedPredicate(auth.GetDefaultDerivedPredicate()). Build() // Create and initialize an IoTDAClient instance. client := iotda.NewIoTDAClient( iotda.IoTDAClientBuilder(). // For the standard or enterprise edition, create a region object. For the basic edition, select the region object in IoTDARegion. WithRegion(region.NewRegion(regionId, endpoint)). // WithRegion(region.CN_NORTH_4). WithCredential(auth). Build()) // Instantiate a request object. request := &model.ListDevicesRequest{} // Call the API for querying the device list. response, err := client.ListDevices(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
Parameter
Description
ak
Access key ID of your Huawei Cloud account. You can create and view an AK/SK on the Access Keys.
page of the Huawei Cloud management console. For more information, seesk
Secret access key (SK) of your Huawei Cloud account.
IoTDARegion.CN_NORTH_4
Region where the IoT platform to be accessed is located. The available regions of the IoT platform have been defined in the SDK code region.go.
On the console, you can view the region name of the current service and the mapping between regions and endpoints. For details, see Platform Connection Information.
Additional Information
For details on the project source code and usage guide, see Huawei Cloud Go Software Development Kit (Go SDK).
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot