Updated on 2026-11-10 GMT+08:00

Go SDK Calling Example

This section uses the API for querying the instance list (ListInstances) as an example to describe how to use the Huawei Cloud Go SDK.

Prerequisites

  • You have obtained the access key (AK) and secret access key (SK) corresponding to your Huawei Cloud account. For details, see Creating Access Keys.
  • Go 1.14 or later has been installed in the environment where the SDK runs.

Installing the SDK

Run the following command in the CLI to install the Huawei Cloud Go SDK: Go SDK 0.1.218 is used as an example. You can obtain the latest SDK version from SDK Center.

go get github.com/huaweicloud/huaweicloud-sdk-go-v3@v0.1.218

Using the SDK

The following is a complete Go example, which shows how to use the Huawei Cloud Go SDK to call the ListInstances API.

  • To obtain the SDK example code for other APIs, see Obtaining SDK Sample Code.
  • This example provides only the basic API parameter settings. For more advanced settings, see the user manual of the SDK.

    There will be huge security risks if the AK and SK used for authentication are directly written into the code. You are advised to encrypt the AK and SK and store them in the configuration file or set environment variables, and decrypt them when using them. In this example, the AK and SK are stored as the environment variables CLOUD_SDK_AK and CLOUD_SDK_SK. For details, see Environment Variables.

    package main
    import (
        "fmt"
        "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        kafka "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/kafka/v2"
        "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/kafka/v2/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/kafka/v2/region"
    )
    func main() {
        // ==================== Initialize the client. ==================== 
        // Obtain the AK/SK from the environment variables. Before running, you need to set the AK/SK as environment variables.
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
        projectId := "195e********4a708ad9f9********1d"
    
        // Configure an authentication credential.
        auth, err := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            WithProjectId(projectId).
            SafeBuild()
        if err != nil {
            fmt.Println(err)
            return
        }
    
        // Initialize the client.
        hcClient, err := kafka.KafkaClientBuilder().
             WithRegion(region.ValueOf("ap-southeast-1")).
             WithCredential(auth).
             SafeBuild()
        if err != nil {
            fmt.Println(err)
            return
        }
        client := kafka.NewKafkaClient(hcClient)
    
        // ==================== Construct a request object. ==================== 
        // Use the request object provided by the SDK to encapsulate request parameters.
        request := &model.ListInstancesRequest{}
        request.Engine = model.GetListInstancesRequestEngineEnum().KAFKA
    
        // ==================== Initiate a request and handle the response. ==================== 
        // Use the client to call the corresponding API. If the API is successfully called, a response object is returned. If the API fails to be called, an error message is returned.
        response, err := client.ListInstances(request)
        if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }