You can use the custom runtime of Function Compute to write functions in Function Compute by using Go. This topic describes how to deploy and invoke a Go function. You can modify the Go sample code to perform secondary development.


The following operations are complete:


  1. Run the following command in the target directory to initialize the project:
    s init start-fc-custom-samples -d start-fc-custom-samples
  2. Run the following command to go to the project directory:
    cd start-fc-custom-samples/golang/event-demo/
  3. Run the following command to install dependencies:
    make build
    Command output:
    docker build -t fc-go-runtime  -f build-image/Dockerfile build-image
    Sending build context to Docker daemon  2.048kB
    Step 1/5 : FROM golang:1.12.16-stretch
     ---> 7ad03a8aece5
    Step 2/5 : RUN mkdir -p $GOPATH/src/
     ---> Using cache
     ---> 8ae05c78f213
    Step 3/5 : RUN cd $GOPATH/src/ && git clone
     ---> Using cache
     ---> 4144ef5b258b
    Step 4/5 : RUN cd $GOPATH/src/ &&  git clone
     ---> Using cache
     ---> d7bcb9bf6bda
    Step 5/5 : RUN go get
     ---> Running in 86b5b13ec231
    Removing intermediate container 86b5b13ec231
     ---> c051b618c0ab
    Successfully built c051b618c0ab
    Successfully tagged fc-go-runtime:latest
    docker run --rm -it -v $(pwd):/tmp fc-go-runtime bash -c "go build -o /tmp/code//bootstrap /tmp/code/main.go"
    chmod +x code/bootstrap
  4. Run the following command to deploy the project:
    s deploy -y
    Sample output:
    [2021-09-09T17:28:46.906] [INFO ] [S-CLI] - Start ...
    [2021-09-09T17:28:47.428] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-09-09T17:28:47.429] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-09T17:28:47.429] [INFO ] [FC-DEPLOY] - Using accessKeyID: 188077086902****
    [2021-09-09T17:28:47.430] [INFO ] [FC-DEPLOY] - Using accessKeySecret: LTAI4G4cwJkK4Rza6xd9****
      Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
    [2021-09-09T17:28:47.684] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-09T17:28:47.901] [INFO ] [FC-DEPLOY] - Checking Function goEventFunc exists
      Make service custom-demo success.
      Make function custom-demo/goEventFunc success.
    [2021-09-09T17:29:29.304] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-09T17:29:29.365] [INFO ] [FC-DEPLOY] - Checking Function goEventFunc exists
    Tips for next step
    * Display information of the deployed resource: s info
    * Display metrics: s metrics
    * Display logs: s logs
    * Invoke remote function: s invoke
    * Remove Service: s remove service
    * Remove Function: s remove function
    * Remove Trigger: s remove trigger
    * Remove CustomDomain: s remove domain
      region: cn-hangzhou
        name: custom-demo
        name: goEventFunc
        runtime: custom
        handler: main.handler
        memorySize: 512
        timeout: 30
  5. Run the following command to invoke the function:
    s invoke -e "{}"
    Sample output:
    [2021-09-09T17:30:32.509] [INFO ] [S-CLI] - Start ...
    ========= FC invoke Logs begin =========
    FC Initialize Start RequestId: 1190d876-343a-41ae-a4e8-705192b1d6b8
    2021-09-09T09:30:33.493Z: 1190d876-343a-41ae-a4e8-705192b1d6b8 [INFO]  init golang!
    FC Initialize End RequestId: 1190d876-343a-41ae-a4e8-705192b1d6b8
    FC Invoke Start RequestId: 1190d876-343a-41ae-a4e8-705192b1d6b8
    2021-09-09T09:30:33.506Z: 1190d876-343a-41ae-a4e8-705192b1d6b8 [INFO]  hello golang!
    FC Invoke End RequestId: 1190d876-343a-41ae-a4e8-705192b1d6b8
    Duration: 5.74 ms, Billed Duration: 6 ms, Memory Size: 512 MB, Max Memory Used: 7.69 MB
    ========= FC invoke Logs end =========
    FC Invoke Result:
    End of method: invoke