Use the Alibaba Cloud Go SDK with an IDE
Get started with the Alibaba Cloud Go SDK using Visual Studio Code (VS Code) on Windows.
Prerequisites
-
Go is installed. Install Go on Windows.
-
VS Code is installed. Set up a Go development environment on Windows.
Use the SDK
Use the sample project from OpenAPI Explorer
The sample project download may fail. If so, Use the SDK in an existing project instead.
-
Go to the API Debugging page in the OpenAPI Portal. Select a cloud product and API. This example uses the ECS DescribeRegions API. Enter DescribeRegions in the search bar and click the API name.

-
On the Parameter Settings tab, enter the required parameters. The Document tab on the right provides the API description, notes, billing information, and parameter details.
The DescribeRegions API supports three parameters:
Parameter name
Required
Description
InstanceChargeType
Optional
Supported regions vary by billing method. Default: PrePaid.
ResourceType
Optional
Supported regions vary by resource type. Default: instance.
AcceptLanguage
Optional
Language of returned results. Default: zh-CN.

-
On the SDK Sample Code tab in the rightmost column, select a programming language and click Download Project to download the complete SDK project to your computer. Then, decompress the package.

-
In VS Code, click File > Open Folder and select the decompressed folder.
-
Click Terminal > New Terminal to open a terminal at the bottom.

-
Run the following command to update module dependencies.
go mod tidy -
Run the following command to execute the sample code in the main package.
go run ./main -
Verify the result. Press
Ctrl+Fin the Terminal and search forstatusCode. A"statusCode": 200response indicates success.
Use the SDK in an existing project
-
In VS Code, click File > Open Folder and create or select a project folder, such as `gosdkproject`.
-
Click Terminal > New Terminal, then run
go mod init gosdkprojectsto initialize the Go project.
-
Obtain the SDK.
In SDK Center, select a cloud product such as ECS, set SDK Version to V2.0, and set Language to Go.

-
Install the SDK.
Copy the installation command into the terminal and run it.

-
Create a .go file. Next to the project name, click New File... and enter a file name, such as `ecsDescribeRegions.go`.

-
Initialize the client.
Initialize the ECS client before calling any ECS API.
ImportantYou must use an AccessKey pair to complete identity verification when you initialize the client. In this case, you must obtain an AccessKey pair in advance. For more information about how to obtain an AccessKey pair, see Create an AccessKey.
After you obtain the AccessKey pair of a RAM user, you must configure the AccessKey pair in environment variables. For more information, see Configure environment variables in Linux, macOS, and Windows.
For more information about how to configure the endpoint, see Endpoints.
package main import ( "os" openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" ecs20140526 "github.com/alibabacloud-go/ecs-20140526/v4/client" "github.com/alibabacloud-go/tea/tea" ) // CreateClient initializes and returns an ECS client. // This function does not accept parameters, but requires that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are set. // return *ecs20140526.Client // return error: Returns a non-nil error object if any error occurs during client creation. func CreateClient() (_result *ecs20140526.Client, _err error) { // Initialize the openapi.Config object to configure the ECS client. config := &openapi.Config{ AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")), AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")), Endpoint: tea.String("ecs.cn-hangzhou.aliyuncs.com"), } // Create and return an ECS client instance using the configuration. return ecs20140526.NewClient(config) } -
Call the API. Review the API Documentation for the target API. The following example calls the `DescribeRegions` API of ECS.
NoteEach API has a separate request object that follows the `${APIName}${Request}` naming convention, such as `DescribeRegionsRequest`.
package main import ( "os" openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" ecs20140526 "github.com/alibabacloud-go/ecs-20140526/v4/client" "github.com/alibabacloud-go/tea/tea" ) // CreateClient initializes and returns an ECS client. // This function does not accept parameters, but requires that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are set. // return *ecs20140526.Client // return error: Returns a non-nil error object if any error occurs during client creation. func CreateClient() (_result *ecs20140526.Client, _err error) { // Initialize the openapi.Config object to configure the ECS client. config := &openapi.Config{ AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")), AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")), Endpoint: tea.String("ecs.cn-hangzhou.aliyuncs.com"), } // Create and return an ECS client instance using the configuration. return ecs20140526.NewClient(config) } // The InvokeApi function calls the DescribeRegions API of ECS to query available region information. // // Return values: // _result: Returns a pointer of the *ecs20140526.DescribeRegionsResponse type, which contains the queried region information. // _err: Returns an error message of the error type. This value is not empty if an error occurs during the call. func InvokeApi()(_result *ecs20140526.DescribeRegionsResponse, _err error) { // Create an ECS client. client, _err := CreateClient() if _err != nil { // If an error occurs during client creation, return the error message directly. return _result, _err } // Create a DescribeRegions request. describeRegionsRequest := &ecs20140526.DescribeRegionsRequest{} // Initiate the DescribeRegions request and return the result. return client.DescribeRegions(describeRegionsRequest) } -
Handle exceptions.
The Alibaba Cloud Go SDK returns errors for standard exception handling (Exception handling). For unexpected situations, use
panicwithdeferandrecover.panicstops execution immediately. Within adeferblock,recovercatches the panic and resumes normal execution.package main import ( "fmt" "os" openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" ecs20140526 "github.com/alibabacloud-go/ecs-20140526/v4/client" "github.com/alibabacloud-go/tea/tea" ) // CreateClient initializes and returns an ECS client. // This function does not accept parameters, but requires that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are set. // return *ecs20140526.Client // return error: Returns a non-nil error object if any error occurs during client creation. func CreateClient() (_result *ecs20140526.Client, _err error) { // Initialize the openapi.Config object to configure the ECS client. config := &openapi.Config{ AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")), AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")), Endpoint: tea.String("ecs.cn-hangzhou.aliyuncs.com"), } // Create and return an ECS client instance using the configuration. return ecs20140526.NewClient(config) } // The InvokeApi function calls the DescribeRegions API of ECS to query available region information. // // Return values: // _result: Returns a pointer of the *ecs20140526.DescribeRegionsResponse type, which contains the queried region information. // _err: Returns an error message of the error type. This value is not empty if an error occurs during the call. func InvokeApi() (_result *ecs20140526.DescribeRegionsResponse, _err error) { // Create an ECS client. client, _err := CreateClient() if _err != nil { // If an error occurs during client creation, return the error message directly. return _result, _err } // Create a DescribeRegions request. describeRegionsRequest := &ecs20140526.DescribeRegionsRequest{} // Initiate the DescribeRegions request and return the result. return client.DescribeRegions(describeRegionsRequest) } // This function obtains region information by calling an API, and recovers from and handles exceptions when they occur. func main() { // Use defer only in scenarios such as deep recursion or unexpected errors. defer func() { if err := tea.Recover(recover()); err != nil { // When an exception is caught, handle it based on the exception type. if sdkError, ok := err.(*tea.SDKError); ok { // Print the SDK error message, error code, and related data. fmt.Println(tea.StringValue(sdkError.Message)) fmt.Println(tea.StringValue(sdkError.Code)) fmt.Println(tea.StringValue(sdkError.Data)) } else { // Print other types of error messages. fmt.Println(err) } } }() // Call the API to get the result. result, _ := InvokeApi() // Traverse and print the region information in the result. for _, region := range result.Body.Regions.Region { fmt.Println("regionId: " + tea.StringValue(region.RegionId)) } // Print the RequestId. fmt.Println("RequestId: " + tea.StringValue(result.Body.RequestId)) } -
In the terminal, run
go runto execute the code.




