本文介绍如何通过Go SDK快速调用API获取服务、创建函数和调用函数等。

前提条件

您已完成以下操作:

Go SDK示例

示例代码如下:
package main

import (
    "fmt"
    "os"

    "github.com/aliyun/fc-go-sdk"
)

func main() {
    serviceName := "service555"
    client, _ := fc.NewClient(os.Getenv("ENDPOINT"), "2016-08-15", os.Getenv("ACCESS_KEY_ID"), os.Getenv("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)
    }

    // 获取服务。
    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)
    }

    // 更新服务。
    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)
    }

    // 更新符合IfMatch参数描述的服务。
    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)
    }

    // 更新不符合IfMatch参数描述的服务。
    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)
    }

    // 获取服务列表。
    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)
    }

    // 创建函数。
    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)
    }

    // 获取函数列表。
    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)
    }

    // 更新函数。
    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)
    }

    // 调用函数。
    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)
    }

    // 发布服务版本。
    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)
    }

    // 发布符合IfMatch参数描述的服务版本。
    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)
    }

    // 发布不符合IfMatch参数描述的服务版本。
    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)
    }

    // 获取版本列表。
    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)
    }

    // 获取符合qualifier参数描述的服务。
    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)
    }

    // 创建别名。
    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)
    }

    // 获取别名。
    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)
    }

    // 更新别名。
    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)
    }

    // 获取别名列表。
    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)
    }

    // 删除别名。
    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)
    }

    // 删除服务版本。
    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)
    }

    // 删除函数。
    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)
            }

        }
    }

    // 删除服务。
    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)
    }

    // 设置预留配置。
    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)
    }

    // 获取预留配置。
    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)
    }

    // 获取预留配置列表。
    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)
    }
}