This topic describes how to use the SDK for Go to manage layers. For example, you can publish and delete a layer version, and query the information about a layer version.

SDK sample code

client, _ := fc.NewClient(
        os.Getenv("ENDPOINT"), "2016-08-15",
        os.Getenv("ACCESS_KEY_ID"),
        os.Getenv("ACCESS_KEY_SECRET"),
        fc.WithTransport(&http.Transport{MaxIdleConnsPerHost: 100}))

    // Specify a layer name. 
    layerName := "test-layer"
    // Prepare a layer file in the ZIP format. 
    layZipFile := "./hello_world.zip"
    // Specify a compatible runtime environment. 
    compatibleRuntime := []string{"python3", "nodejs12"}

    // Publish a layer version. 
    fmt.Println("Publish layer versions")
    data, err := ioutil.ReadFile(layZipFile)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        return
    }
    publishLayerVersionOutput, err := client.PublishLayerVersion(fc.NewPublishLayerVersionInput().
        WithLayerName(layerName).
        WithCode(fc.NewCode().WithZipFile(data)).
        WithCompatibleRuntime(compatibleRuntime).
        WithDescription("my layer"),
    )
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("PublishLayerVersion response: %+v \n\n", publishLayerVersionOutput)
    }

    // Query the information about a specified layer version. 
    fmt.Printf("Get the layer of version %d\n", publishLayerVersionOutput.Layer.Version)
    getLayerVersionOutput, err := client.GetLayerVersion(
        fc.NewGetLayerVersionInput(layerName, publishLayerVersionOutput.Layer.Version))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("GetLayerVersion response: %+v \n\n", getLayerVersionOutput.Layer)
    }

    // Query layers. 
    fmt.Println("List layers")
    nextToken := ""
    layers := []*fc.Layer{}
    for {
        listLayersOutput, err := client.ListLayers(
            fc.NewListLayersInput().
                WithLimit(100).
                WithNextToken(nextToken))
        if err != nil {
            fmt.Fprintln(os.Stderr, err)
            break
        }
        if len(listLayersOutput.Layers) != 0 {
            layers = append(layers, listLayersOutput.Layers...)
        }
        if listLayersOutput.NextToken == nil {
            break
        }
        nextToken = *listLayersOutput.NextToken

    }
    fmt.Println("ListLayers response:")
    for _, layer := range layers {
        fmt.Printf("- layerName: %s, layerMaxVersion: %d\n", layer.LayerName, layer.Version)
    }

    // Query the versions of a layer. 
    fmt.Println("List layer versions")
    // The initial version of the layer, which starts from 1 by default. 
    startVersion := int32(1)
    fmt.Println("ListLayerVersions response:")
    layerVersions := []*fc.Layer{}
    for {
        listLayerVersionsOutput, err := client.ListLayerVersions(
            fc.NewListLayerVersionsInput(layerName, startVersion).
                WithLimit(100))
        if err != nil {
            if err, ok := err.(*fc.ServiceError); ok &&
                err.HTTPStatus == http.StatusNotFound {
                break
            }
            fmt.Fprintln(os.Stderr, err)
            break
        }
        if len(listLayerVersionsOutput.Layers) > 0 {
            layerVersions = append(layerVersions, listLayerVersionsOutput.Layers...)
        }
        if listLayerVersionsOutput.NextVersion == nil ||
            *listLayerVersionsOutput.NextVersion == 0 {
            break
        }
        startVersion = *listLayerVersionsOutput.NextVersion
    }

    for _, layer := range layerVersions {
        fmt.Printf("- layerName: %s, layerVersion: %d\n", layer.LayerName, layer.Version)
    }

    // Delete a layer version. 
    fmt.Printf("Delete the layer of version %d \n", publishLayerVersionOutput.Layer.Version)
    deleteLayerVersionOutput, err := client.DeleteLayerVersion(
        fc.NewDeleteLayerVersionInput(layerName, publishLayerVersionOutput.Layer.Version))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    } else {
        fmt.Printf("DeleteLayerVersion response: %+v \n\n", deleteLayerVersionOutput)
    }