All Products
Search
Document Center

SDK for Go

Last Updated: Mar 30, 2021

SDK for Go

Procedure

To use AIRec SDK for Go, perform the following steps:

Step 1: Create an Alibaba Cloud account

For more information, see Sign up with Alibaba Cloud. We recommend that you complete real-name verification at your earliest opportunity. Otherwise, you cannot use some Alibaba Cloud services.

Step 2: Obtain an Alibaba Cloud AccessKey pair

Before you use AIRec SDK for Java, you must apply for an Alibaba Cloud AccessKey pair.

Go to the Security Management page. Select an AccessKey pair for AIRec SDK for Java. The AccessKey pair consists of an AccessKey ID and an AccessKey secret. If no AccessKey pair is available, create one and make sure that it is in the Enable state. For more information about how to create an AccessKey pair, see Preparations.

Step 3: Install the Go development environment

We recommend that you use Go 1.5 or later for AIRec SDK. You can visit the Go official website to download and install the Go development environment.

Step 4: Install AIRec SDK for Go.

Obtain and Install AIRec SDK for Go of the latest version.

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

AIRec-related protocol code is stored in the services/airec directory.

Step 5: Use AIRec SDK for Go

The following sample code describes how to use AIRec SDK for Go to create an instance:

package main

import (
    "fmt"

    "github.com/aliyun/alibaba-cloud-sdk-go/services/airec"
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk/endpoints"
)

func main() {

    var accessKeyID = "xxx"
    var accessKeySecret = "xxx"

    // Initialize configurations.
    endpoints.AddEndpointMapping("cn-hangzhou", "Airec", "airec.cn-hangzhou.aliyuncs.com")
    // Create a client.
    client, err := airec.NewClientWithAccessKey("cn-hangzhou", accessKeyID, accessKeySecret)
    if err ! = nil {
        panic(err)
    }

    // Create a request object.
    request := airec.CreateRecommendRequest()
    request.SetInstanceId("airec-cn-xxx")
    request.SetUserId("102")
    request.SetReturnCoun(10)

    request.SetContentType("application/json;charset=utf-8")

    // Obtain responses.
    response, err := client.Recommend(request)
    if err ! = nil {
        panic(err)
    }
    fmt.Println(response)
}