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

Go SDK Usage Example

This section describes how to use the Huawei Cloud Go SDK by calling the CreateInstanceNamespace API of SWR.

Prerequisites

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

Installing the SDK

Run the command below in the CLI to install the Huawei Cloud Go SDK. The 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 example demonstrating how to use the Huawei Cloud Go SDK to call the CreateInstanceNamespace 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 written into the code. You are advised to encrypt the AK and SK and store them in a configuration file or environment variables, and decrypt them when needed to ensure security. 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"
            "os"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        swr "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/swr/v2"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/swr/v2/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/swr/v2/region"
    )
    
    func main() {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
    
        auth, err := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            SafeBuild()
    
        if err != nil {
            fmt.Println(err)
            return
        }
    
        hcClient, err := swr.SwrClientBuilder().
             WithRegion(region.ValueOf("cn-north-4")).
             WithCredential(auth).
             SafeBuild()
    
    
        if err != nil {
            fmt.Println(err)
            return
        }
    
        client := swr.NewSwrClient(hcClient)
    
        request := &model.CreateInstanceNamespaceRequest{}
    	request.InstanceId = "instance_id"
    	metadatabody := &model.NamespaceMetadata{
    		Public: model.GetNamespaceMetadataPublicEnum().TRUE,
    	}
    	request.Body = &model.CreateInstanceNamespaceRequestBody{
    		Metadata: metadatabody,
    		NamespaceName: "test",
    	}
    	response, err := client.CreateInstanceNamespace(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }