Updated on 2024-03-28 GMT+08:00

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

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

  2. Install the Huawei Cloud Go library.

    go get github.com/huaweicloud/huaweicloud-sdk-go-v3

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

  1. Create a credential.
  2. Create and initialize an IoTDAClient instance.
  3. Instantiate a request object.
  4. 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 My Credentials > Access Keys page of the Huawei Cloud management console. For more information, see Access Keys.

    sk

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