This topic describes how to use SDK for Go.

Prerequisites

Your system needs to meet the environment requirements. For example, Go 1.10.x or later has been installed in your system.

Installation

Run the go get command to download and install SDK for Go.

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

If you use glide to manage dependencies, you can also use it to install Alibaba Cloud SDK for Go.

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

Alibaba Cloud SDK for Go is also released at https://develop.aliyun.com/tools/sdk#/go.

Quick start

You must have an Alibaba Cloud account and an AccessKey pair to use SDK for Go. The following example shows how to use SDK for Go to call Serverless workflow. In this example, a flow is created, an execution is initiated, and the execution details are queried.

Request method

package main

import (
    "fmt"
    "time"

    "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/fnf"
)

var (
    flowDefinitionType = "FDL"
    flowName           = "xxx"
    flowDefinition     = `xxx`
    flowDescription    = "some descriptions"
    roleArn            = "acs:ram::${Your_Account_ID}:${Your_Role}"
    executionName      = "xxx"
)

// CreateFlow ...
func CreateFlow(cli *fnf.Client) (*fnf.CreateFlowResponse, error) {
    request := fnf.CreateCreateFlowRequest()
    request.Name = flowName
    request.Definition = flowDefinition
    request.Description = flowDescription
    request.Type = flowDefinitionType
    request.RoleArn = roleArn
    return cli.CreateFlow(request)
}

// StartExecution ...
func StartExecution(cli *fnf.Client) (*fnf.StartExecutionResponse, error) {
    request := fnf.CreateStartExecutionRequest()
    request.FlowName = flowName
    request.ExecutionName = executionName
    return cli.StartExecution(request)
}

// DescribeExecution ...
func DescribeExecution(cli *fnf.Client) (*fnf.DescribeExecutionResponse, error) {
    request := fnf.CreateDescribeExecutionRequest()
    request.FlowName = flowName
    request.ExecutionName = executionName
    return cli.DescribeExecution(request)
}

// GetExecutionHistory ...
func GetExecutionHistory(cli *fnf.Client) (*fnf.GetExecutionHistoryResponse, error) {
    request := fnf.CreateGetExecutionHistoryRequest()
    // request.Limit and request.NextToken can set here. For easy demo, we passed.
    request.FlowName = flowName
    request.ExecutionName = executionName
    return cli.GetExecutionHistory(request)
}

Create a client and use the preceding functions to initiate a series of calls.

Note If you need to implement debugging without modification, place the following function in the same file as the preceding request method code block to avoid errors upon import.
func main() {
    fnfCli, err := fnf.NewClientWithAccessKey("cn-hangzhou", "AccessID", "AccessKey")
    if err ! = nil {
        panic(err)
    }
    // Create a flow
    _, err = CreateFlow(fnfCli)
    if err ! = nil {
        panic(err)
    }
    // StartExecution
    _, err = StartExecution(fnfCli)
    if err ! = nil {
        panic(err)
    }
    time.Sleep(time.Second)
    // DescribeExecution
    desResp, err := DescribeExecution(fnfCli)
    if err ! = nil {
        panic(err)
    }
    fmt.Println(fmt.Sprintf("%s status: %s", desResp.Name, desResp.Status))
    // GetExecutionHistory
    _, err = GetExecutionHistory(fnfCli)
    if err ! = nil {
        panic(err)
    }
}