Go SDK examples
This topic uses the Go SDK V2.0 as an example to demonstrate how to call Cloud Monitor 2.0 (cms20240330) OpenAPI operations. The calling method is similar for other operations. You only need to replace the request and response types and fields.
Prerequisite
Go 1.10.x or later.
Installation
Run the following commands in your project directory:
go get github.com/alibabacloud-go/cms-20240330/v10
go get github.com/alibabacloud-go/darabonba-openapi/v2/client
go get github.com/alibabacloud-go/tea-utils/v2/service
go get github.com/aliyun/credentials-go/credentials
go get github.com/alibabacloud-go/tea/teaSource code repository: alibabacloud-go/cms-20240330.
Configure access credentials
We recommend that you use a more secure method that does not require an AccessKey pair for your project code. For more information about credential configuration, see Manage access credentials. The simplest approach is to configure environment variables:
export ALIBABA_CLOUD_ACCESS_KEY_ID="<your-access-key-id>"
export ALIBABA_CLOUD_ACCESS_KEY_SECRET="<your-access-key-secret>"credentials.NewCredential(nil) searches for credentials in the order defined by the default credential chain.
Complete code example
The following example calls the GetCmsService operation to query the activation status of a Cloud Monitor 2.0 service for a specific product:
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
cms20240330 "github.com/alibabacloud-go/cms-20240330/v10/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"
credential "github.com/aliyun/credentials-go/credentials"
)
// Initialize the Client using credentials.
func CreateClient() (_result *cms20240330.Client, _err error) {
// In production, we recommend using a more secure, AccessKey-free method.
cred, _err := credential.NewCredential(nil)
if _err != nil {
return _result, _err
}
config := &openapi.Config{
Credential: cred,
}
// For the endpoint, see https://api.aliyun.com/product/Cms.
config.Endpoint = tea.String("metrics.cn-hangzhou.aliyuncs.com")
_result, _err = cms20240330.NewClient(config)
return _result, _err
}
func _main(args []*string) (_err error) {
client, _err := CreateClient()
if _err != nil {
return _err
}
getCmsServiceRequest := &cms20240330.GetCmsServiceRequest{
Product: tea.String("your_value"),
Service: tea.String("your_value"),
}
headers := make(map[string]*string)
tryErr := func() (_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
resp, _err := client.GetCmsServiceWithOptions(getCmsServiceRequest, headers, &util.RuntimeOptions{})
if _err != nil {
return _err
}
fmt.Printf("[LOG] %v\n", resp)
return nil
}()
if tryErr != nil {
var err = &tea.SDKError{}
if _t, ok := tryErr.(*tea.SDKError); ok {
err = _t
} else {
err.Message = tea.String(tryErr.Error())
}
// Error message
fmt.Println(tea.StringValue(err.Message))
// Troubleshooting URL
var data interface{}
d := json.NewDecoder(strings.NewReader(tea.StringValue(err.Data)))
d.Decode(&data)
if m, ok := data.(map[string]interface{}); ok {
recommend, _ := m["Recommend"]
fmt.Println(recommend)
}
}
return _err
}
func main() {
err := _main(tea.StringSlice(os.Args[1:]))
if err != nil {
panic(err)
}
}Procedure
The code works as follows:
Import the required dependency packages:
import ( cms20240330 "github.com/alibabacloud-go/cms-20240330/v10/client" openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" )Initialize the Config object
&openapi.Config. The Config object stores the Credential, Endpoint, and other configurations. The Endpoint ismetrics.cn-hangzhou.aliyuncs.comas shown in the example.cred, _ := credential.NewCredential(nil) config := &openapi.Config{ Credential: cred, } config.Endpoint = tea.String("metrics.cn-hangzhou.aliyuncs.com")Instantiate the client:
client, _ := cms20240330.NewClient(config)Create the Request object for the API operation:
request := &cms20240330.GetCmsServiceRequest{}Set request parameters:
request.Product = tea.String("your_value") request.Service = tea.String("your_value")Call the API operation through the client:
response, err := client.GetCmsService(request) fmt.Println(response)Handle errors:
if err != nil { fmt.Println(err) return }Read return values from the response, for example:
fmt.Println(tea.StringValue(response.Body.RequestId))
For more API operations and sample code, see SDK examples on the OpenAPI portal.