This topic describes how to install and use the Go SDK, using the GetProject operation of Function AI as an example. For more information about the GetProject API, see GetProject.
Prerequisites
You need an AccessKey pair to call Alibaba Cloud OpenAPI operations. An AccessKey pair for an Alibaba Cloud account provides full access to all resources and poses a high security risk if leaked. We recommend that you create and use an AccessKey pair for a Resource Access Management (RAM) user that has only the required permissions.
Configure the AccessKey pair in environment variables
For more information, see Configure environment variables on Linux, macOS, and Windows.
Environment requirements
Your Go environment must be version 1.10.x or later.
Install the SDK
Run the following command to install the Function AI SDK:
go get github.com/alibabacloud-go/devs-20230714Use the SDK
1. Initialize the client
In the SDK, all OpenAPI calls are made through a client object. Before you call an OpenAPI operation, you must initialize a client. You can initialize the client in several ways. This example shows how to initialize a client using an AccessKey pair. For more information about other initialization methods, see Manage access credentials.
package main
import (
devs20230714 "github.com/alibabacloud-go/devs-20230714/v2/client"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
"github.com/alibabacloud-go/tea/tea"
"os"
)
func CreateClient() (*devs20230714.Client, error) {
return devs20230714.NewClient(&openapi.Config{
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is set in your runtime environment.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is set in your runtime environment.
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
// The endpoint to access.
Endpoint: tea.String("devs.cn-hangzhou.aliyuncs.com"),
})
}2. Build a request object
Before you build a request object, check the API documentation for the operation to obtain parameter information.
The request object follows the naming convention: {APIName}Request. For example, the request object for the GetProject operation is GetProjectRequest.
// Create a request object.
GetProjectRequest := &devs20230714.GetProjectRequest{
ProjectName: tea.String("your_project_name"),
}3. Make a call
When you call an OpenAPI operation using the client, you can set runtime parameters, such as timeout and proxy settings. For more information, see Advanced configurations.
The response object follows the naming convention: {APIName}Response. For example, the response object for the GetProject operation is GetProjectResponse.
// Set runtime parameters.
runtime := &util.RuntimeOptions{}
// Call the GetProject operation.
response, _err := Client.GetProjectWithOptions(GetProjectRequest, runtime)
if _err != nil {
panic(_err)
}
fmt.Println(response.Body.String())4. Handle exceptions
The Go SDK classifies exceptions into the following main error types:
error: A non-business error, such as a validation error caused by modified SDK source files or a parsing error.
SDKError: A business error that occurs during an SDK request.
For more information about exception handling, see Exception handling.
Take appropriate measures to handle exceptions, such as propagating exceptions, recording logs, and attempting to recover. This helps ensure the robustness and stability of your system.
5. Complete example
package main
import (
"encoding/json"
"strings"
"fmt"
"os"
devs20230714 "github.com/alibabacloud-go/devs-20230714/v2/client"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
)
func CreateClient() (*devs20230714.Client, error) {
return devs20230714.NewClient(&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("devs.cn-hangzhou.aliyuncs.com"),
})
}
func _main (args []*string) (_err error) {
client, _err := CreateClient()
if _err != nil {
return _err
}
runtime := &util.RuntimeOptions{}
headers := make(map[string]*string)
tryErr := func()(_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// To run this code, copy it and print the API return value.
_, _err = client.GetProjectWithOptions(tea.String("your_project_name"), headers, runtime)
if _err != nil {
return _err
}
return nil
}()
if tryErr != nil {
var error = &tea.SDKError{}
if _t, ok := tryErr.(*tea.SDKError); ok {
error = _t
} else {
error.Message = tea.String(tryErr.Error())
}
// This code is for demonstration only. Handle exceptions with care in your projects. Do not ignore them.
// Error message
fmt.Println(tea.StringValue(error.Message))
// Diagnosis address
var data interface{}
d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
d.Decode(&data)
if m, ok := data.(map[string]interface{}); ok {
recommend, _ := m["Recommend"]
fmt.Println(recommend)
}
_, _err = util.AssertAsString(error.Message)
if _err != nil {
return _err
}
}
return _err
}
func main() {
err := _main(tea.StringSlice(os.Args[1:]))
if err != nil {
panic(err)
}
}More information
In addition to the standard SDK method, you can use generalized calls to call Function AI OpenAPI operations. For more information, see Generalized calls.