This topic describes the structure and features of the Go initializer function.

Definition

An initializer function is used to ensure that functions are executed on the same instance only once.

Note The meaning of success and only once is that even if there is a situation where the execution is performed twice, such as the first failure and the second success, it will only be retried once.

Function definition, with only one context parameter, uses the same method as an event function.

function(ctx context.Context)

Initializer function use example

Using Initializer requires two steps:

  1. In the code, register an Initializer function through the fc.RegistryInitializerFunction(Init).
  2. In Function Configuration, configure Initializer-related information. For more information, see Function management.

The following sample code shows you how to specify table names and column names in the CREATE TABLE statement:

package main

import (
    "context"
    "log"
    "github.com/aliyun/fc-runtime-go-sdk/fc"
)

var (
    count int = 1
)

func Init(ctx context.Context) {
    count += 1000
}

func main() {
    fc.RegisterInitializerFunction(Init)
    fc.Start(HandleRequest)
}

func HandleRequest() (int, error) {
    count += 1
    log.Println("count: ", count)
    return count, nil
}