This topic shows you how to perform operations by using Function Compute SDK for Go to call the required API operations with efficiency. For example, you can query the details of a service, create a function, and invoke a function.

Prerequisites

Before you use the SDK for Go, make sure that the following operations are complete:

Sample code of the SDK for Go

The following code provides examples on how to use the SDK for Go to perform different operations:
package main

import (
    "fmt"
    "os"
    "github.com/aliyun/fc-go-sdk"
)

func main() {
    serviceName := "service555"
    /*
    The AccessKey pair of an Alibaba Cloud account can be used to access all API operations. Using these credentials to perform operations in Function Compute is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. 
    We recommend that you do not save the AccessKey ID and AccessKey secret to your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised. 
    In this example, the AccessKey pair is saved to the environment variables for authentication. 
    Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables in your local environment before you run the sample code. 
    In the runtime environments of Function Compute, the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are automatically configured after you configure the execution permissions. 
    */
    client, _ := fc.NewClient(os.Getenv("ENDPOINT"), "2016-08-15", os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))

    fmt.Println("Creating service")
    createServiceOutput, err := client.CreateService(fc.NewCreateServiceInput().
        WithServiceName(serviceName).
        WithDescription("this is a smoke test for go sdk"))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    }
    if createServiceOutput != nil {
        fmt.Printf("CreateService response: %s \n", createServiceOutput)
    }

    // Query a service. 
    fmt.Println("Getting service")
    getServiceOutput, err := client.GetService(fc.NewGetServiceInput(serviceName))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("GetService response: %s \n", getServiceOutput)
    }

    // Update a service. 
    fmt.Println("Updating service")
    updateServiceInput := fc.NewUpdateServiceInput(serviceName).WithDescription("new description")
    updateServiceOutput, err := client.UpdateService(updateServiceInput)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("UpdateService response: %s \n", updateServiceOutput)
    }

    // Update a service that complies to the IfMatch parameter. 
    fmt.Println("Updating service with IfMatch")
    updateServiceInput2 := fc.NewUpdateServiceInput(serviceName).WithDescription("new description2").
        WithIfMatch(updateServiceOutput.Header.Get("ETag"))
    updateServiceOutput2, err := client.UpdateService(updateServiceInput2)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("UpdateService response: %s \n", updateServiceOutput2)
    }

    // Update a service that does not comply to the IfMatch parameter. 
    fmt.Println("Updating service with wrong IfMatch")
    updateServiceInput3 := fc.NewUpdateServiceInput(serviceName).WithDescription("new description2").
        WithIfMatch("1234")
    updateServiceOutput3, err := client.UpdateService(updateServiceInput3)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("UpdateService response: %s \n", updateServiceOutput3)
    }

    // Query services. 
    fmt.Println("Listing services")
    listServicesOutput, err := client.ListServices(fc.NewListServicesInput().WithLimit(100))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("ListServices response: %s \n", listServicesOutput)
    }

    // Create a function. 
    fmt.Println("Creating function1")
    createFunctionInput1 := fc.NewCreateFunctionInput(serviceName).WithFunctionName("testf1").
        WithDescription("go sdk test function").
        WithHandler("main.my_handler").WithRuntime("python2.7").
        WithCode(fc.NewCode().WithFiles("./testCode/hello_world.zip")).
        WithTimeout(5)

    createFunctionOutput, err := client.CreateFunction(createFunctionInput1)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("CreateFunction response: %s \n", createFunctionOutput)
    }
    fmt.Println("Creating function2")
    createFunctionOutput2, err := client.CreateFunction(createFunctionInput1.WithFunctionName("testf2"))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("CreateFunction response: %s \n", createFunctionOutput2)
    }

    // Query functions. 
    fmt.Println("Listing functions")
    listFunctionsOutput, err := client.ListFunctions(fc.NewListFunctionsInput(serviceName).WithPrefix("test"))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("ListFunctions response: %s \n", listFunctionsOutput)
    }

    // Update a function. 
    fmt.Println("Updating function")
    updateFunctionOutput, err := client.UpdateFunction(fc.NewUpdateFunctionInput(serviceName, "testf1").
        WithDescription("newdesc"))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("UpdateFunction response: %s \n", updateFunctionOutput)
    }

    // Invoke a function. 
    fmt.Println("Invoking function, log type Tail")
    invokeInput := fc.NewInvokeFunctionInput(serviceName, "testf1").WithLogType("Tail")
    invokeOutput, err := client.InvokeFunction(invokeInput)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("InvokeFunction response: %s \n", invokeOutput)
        logResult, err := invokeOutput.GetLogResult()
        if err != nil {
            fmt.Printf("Failed to get LogResult due to %v\n", err)
        } else {
            fmt.Printf("Invoke function LogResult %s \n", logResult)
        }
    }

    fmt.Println("Invoking function, log type None")
    invokeInput = fc.NewInvokeFunctionInput(serviceName, "testf1").WithLogType("None")
    invokeOutput, err = client.InvokeFunction(invokeInput)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("InvokeFunction response: %s \n", invokeOutput)
    }

    // Publish a service version. 
    fmt.Println("Publishing service version")
    publishServiceVersionInput := fc.NewPublishServiceVersionInput(serviceName)
    publishServiceVersionOutput, err := client.PublishServiceVersion(publishServiceVersionInput)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("PublishServiceVersion response: %s \n", publishServiceVersionOutput)
    }

    // Publish a service version that complies to the IfMatch parameter. 
    fmt.Println("Publishing service version with IfMatch")
    publishServiceVersionInput2 := fc.NewPublishServiceVersionInput(serviceName).
        WithIfMatch(getServiceOutput.Header.Get("ETag"))
    publishServiceVersionOutput2, err := client.PublishServiceVersion(publishServiceVersionInput2)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("PublishServiceVersion response: %s \n", publishServiceVersionOutput2)
    }

    // Publish a service version that does not comply to the IfMatch parameter. 
    fmt.Println("Publishing service with wrong IfMatch")
    publishServiceVersionInput3 := fc.NewPublishServiceVersionInput(serviceName).
        WithIfMatch("1234")
    publishServiceVersionOutput3, err := client.PublishServiceVersion(publishServiceVersionInput3)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("PublishServiceVersion response: %s \n", publishServiceVersionOutput3)
    }

    // Query the versions of a service. 
    fmt.Println("Listing service versions")
    listServiceVersionsOutput, err := client.ListServiceVersions(fc.NewListServiceVersionsInput(serviceName).WithLimit(10))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("ListServiceVersions response: %s \n", listServiceVersionsOutput)
    }

    // Query a service that complies to the qualifier parameter. 
    fmt.Println("Getting service with qualifier")
    getServiceOutput2, err := client.GetService(fc.NewGetServiceInput(serviceName).WithQualifier(*publishServiceVersionOutput.VersionID))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("GetService with qualifier response: %s \n", getServiceOutput2)
    }

    // Create a service alias. 
    aliasName := "alias"
    fmt.Println("Creating alias")
    createAliasOutput, err := client.CreateAlias(fc.NewCreateAliasInput(serviceName).WithAliasName(aliasName).WithVersionID(*publishServiceVersionOutput.VersionID))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("CreateAlias response: %s \n", createAliasOutput)
    }

    // Query a service alias. 
    fmt.Println("Getting alias")
    getAliasOutput, err := client.GetAlias(fc.NewGetAliasInput(serviceName, aliasName))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("GetAlias response: %s \n", getAliasOutput)
    }

    // Update a service alias. 
    fmt.Println("Updating alias")
    updateAliasOutput, err := client.UpdateAlias(fc.NewUpdateAliasInput(serviceName, aliasName).WithVersionID(*publishServiceVersionOutput2.VersionID))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("UpdateAlias response: %s \n", updateAliasOutput)
    }

    // Query the aliases of a service. 
    fmt.Println("Listing aliases")
    listAliasesOutput, err := client.ListAliases(fc.NewListAliasesInput(serviceName))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("ListAliases response: %s \n", listAliasesOutput)
    }

    //Delete a service alias. 
    fmt.Println("Deleting aliases")
    deleteAliasOutput, err := client.DeleteAlias(fc.NewDeleteAliasInput(serviceName, aliasName))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("DeleteAlias response: %s \n", deleteAliasOutput)
    }

    // Delete a service version. 
    fmt.Println("Deleting service version")
    deleteServiceVersionOutput, err := client.DeleteServiceVersion(fc.NewDeleteServiceVersionInput(serviceName, *publishServiceVersionOutput.VersionID))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("DeleteServiceVersion response: %s \n", deleteServiceVersionOutput)
    }

    deleteServiceVersionOutput2, err := client.DeleteServiceVersion(fc.NewDeleteServiceVersionInput(serviceName, *publishServiceVersionOutput2.VersionID))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("DeleteServiceVersion response: %s \n", deleteServiceVersionOutput2)
    }

    // Delete a function. 
    fmt.Println("Deleting functions")
    listFunctionsOutput, err = client.ListFunctions(fc.NewListFunctionsInput(serviceName).WithLimit(10))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("ListFunctions response: %s \n", listFunctionsOutput)
        for _, fuc := range listFunctionsOutput.Functions {
            fmt.Printf("Deleting function %s \n", *fuc.FunctionName)
            if output, err := client.DeleteFunction(fc.NewDeleteFunctionInput(serviceName, *fuc.FunctionName)); err != nil {
                fmt.Fprintln(os.Stderr, err)
            } else {
                fmt.Printf("DeleteFunction response: %s \n", output)
            }

        }
    }

    // Delete a service. 
    fmt.Println("Deleting service")
    deleteServiceOutput, err := client.DeleteService(fc.NewDeleteServiceInput(serviceName))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("DeleteService response: %s \n", deleteServiceOutput)
    }

    // Configure a provisioned setting. 
    fmt.Println("Putting provision config")
    putProvisionConfigOutput, err := client.PutProvisionConfig(fc.NewPutProvisionConfigInput(serviceName, "testAliasName", "testFunctionName").WithTarget(int64(100)))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("PutProvisionConfig response: %s \n", putProvisionConfigOutput)
    }

    // Query a provisioned setting. 
    fmt.Println("Getting provision config")
    getProvisionConfigOutput, err := client.GetProvisionConfig(fc.NewGetProvisionConfigInput(serviceName, "testAliasName", "testFunctionName"))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("GetProvisionConfig response: %s \n", getProvisionConfigOutput)
    }

    // Query provisioned settings. 
    fmt.Println("Listing provision configs")
    listProvisionConfigsOutput, err := client.ListProvisionConfigs(fc.NewListProvisionConfigsInput())
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("ListProvisionConfigs response: %s \n", listProvisionConfigsOutput)
    }
}