edit-icon download-icon

Use Go SDK

Last Updated: Mar 20, 2018

Install Go SDK

Alibaba Cloud Go SDK supports Go 1.7 or later. You can install Go SDK using the following methods:

  • Use Glide (recommended)

    If you use Glide to manage project dependencies, run the following commands to install Go SDK:

    1. glide get github.com/aliyun/alibaba-cloud-sdk-go
  • Use govendor

    Run the following command to install Go SDK:

    1. go get -u github.com/aliyun/alibaba-cloud-sdk-go/sdk

Set up credentials

When using the Alibaba Cloud Go SDK to access Alibaba Cloud services, you need an Alibaba Cloud account for authentication.

Go SDK supports the following authentication methods.

Authentications Descriptions
AccessKey Use the AccessKeyID/Secret to do the authentication
StsToken Use the STS Token to do the authentication
RamRoleArn Use the AssumeRole of the RAM account to do the authentication
EcsRamRole Use the RAM role of an ECS instance to do the authentication
RsaKeyPair Use the RSA key pair to do the authentication (supported only on Japan site)

This document uses AccessKey as an example to illustrate how to set up credentials.

Note: To ensure the security of your account, we recommend using your RAM account instead of the primary account. The primary account has full access to all of your cloud services, while the RAM account has limited access granted by the primary account to the cloud services.

Firstly, create an AccessKey as described in Create an AccessKey, and then set up your credentials when initializing AcsClient as follows:

Note: Do not disclose any code containing your AccessKey (do not commit the code to public GitHub projects). Otherwise, your Alibaba Cloud account may be compromised.

  1. // Create the ecsClient instance
  2. ecsClient, err := ecs.NewClientWithAccessKey(
  3. "<your-region-id>", // Your region ID
  4. "<your-access-key-id>", // Your AccessKey ID
  5. "<your-access-key-secret>") // Your AccessKey Secret

If you want to use a custom configuration or credentials other than AccessKey, you must first create a Credential object to save the credentials, and then submit the Credential object when initiating the client.

  1. // Customize config
  2. config := NewConfig()
  3. // Create a credential object
  4. credential := &credentials.BaseCredential{
  5. AccessKeyId: "<your-access-key-id>",
  6. AccessKeySecret: "<your-access-key-secret>",
  7. }
  8. // Initiate the client
  9. client, err := NewClientWithOptions("cn-hangzhou", config, credential)

Call a service

This document uses ECS as an example to illustrate how to use Alibaba Cloud Go SDK to initiate a request:

  1. Import Alibaba Cloud Go SDK.

    1. import github.com/aliyun/alibaba-cloud-sdk-go/services/ecs
  2. Create an ECS client.

    1. ecsClient := ecs.NewClientWithAccessKey(
    2. "<your-region-id>",
    3. "<your-access-key-id>",
    4. "<your-access-key-secret>")
  3. Use the xxx.CreateXXXRequest method to create a request, and assign values to related request parameters.

    The naming convention for the method is ${service}.Create${apiName}Request,

    where:

    • ${service} is the product name in lower case, such as ecs, oss, and slb.

    • ${apiName} is the API name, such as DescribeInstanceAttribute.

    1. request := ecs.CreateDescribeInstanceAttributeRequest()
    2. request.InstanceId = "<instanceId>"
  4. Initiate a call.

    1. response, err := ecsClient.DescribeInstanceAttribute(request)
  5. Handle the response.

    All the returned attributes are deserialized into the response. You can directly call response.xxx to obtain the response attributes.

    1. instanceStatus := response.Status

    If an error (err != nil) occurs or you want to get the original HTTP response, you can obtain the original data as follows:

    If the status code returned by the server is not in the range of [200,300), ServerError is returned.

    1. // Obtain the original http response, and the response type is *http.Response
    2. httpResponse := response.GetOriginHttpResponse()
    3. // Obtain the status code of the original http response, and the response type is int
    4. statusCode := response.GetHttpStatus()
    5. // Obtain the header of the original http response, and the response type is map[string][]string
    6. headers := response.GetHttpHeaders()
    7. // Obtain the content of the original http response, and the response type is []byte
    8. contentBytes := response.GetHttpContentBytes()
    9. // Obtain the content of the original http response and transcode it to string according to UTF-8, and the response type is string.
    10. contentString := response.GetHttpContentString()
Thank you! We've received your feedback.