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

Procedure

  1. Run the following command to initialize a project:
    s init fc-custom-golang-event -d fc-custom-golang-event
  2. Run the following command to go to the project directory:
    cd fc-custom-golang-event
  3. Optional: Modify the sample code as needed for custom development.
  4. Run the following command to deploy the project:
    s deploy -y
    Sample command output:
    [2022-02-22 08:39:41] [INFO] [S-CLI] - Start the pre-action
    [2022-02-22 08:39:41] [INFO] [S-CLI] - Action: make build
    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 5/5 : RUN go get github.com/awesome-fc/golang-runtime
     ---> Using cache
     ---> 74746b35f154
    Successfully built 74746b35f154
    Successfully tagged fc-go-runtime:latest
    docker run --rm -it -v $(pwd):/tmp fc-go-runtime bash -c "GOARCH=amd64 go build -o /tmp/code//bootstrap /tmp/code/main.go"
    chmod +x code/bootstrap
    [2022-02-22 08:39:48] [INFO] [S-CLI] - End the pre-action
    
    Function [goEventFunc] was changed, please confirm before deployment:
        * You can also specify to use local configuration through --use-local during deployment)
    
     Code check:
    Code package has changed in other ways(checksum):
    Last local deployment -> Online status:null -> 10552998901932247431
    
     Remote function: goEventFunc is inconsistent with the config you deployed last time, deploy it with local config o
    r remote config? use local
     Checking Function goEventFunc exists
     Checking Service, Function (3.69s)
     Creating Service, Function (4.82s)
    
    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
    
    helloworld:
      region:   cn-hangzhou
      service:
        name: hello-world-service
      function:
        name:       goEventFunc
        runtime:    custom
        handler:    index.handler
        memorySize: 512
        timeout:    30
  5. Run the following command to remotely invoke the function:
    s invoke -e "{}"
    Sample command output:
    Please select an access: test
    [2022-02-22 08:40:15] [WARN] [S-CORE] -
    
    If you don't want to select access every time, configure it in yaml: https://github.com/Serverless-Devs/Serverless-Devs/discussions/149
    
    
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: fd3066ff-be85-487a-aa0a-d5eca929be33
    2022-02-22T08:40:16.583Z: fd3066ff-be85-487a-aa0a-d5eca929be33 [INFO]  hello golang!
    FC Invoke End RequestId: fd3066ff-be85-487a-aa0a-d5eca929be33
    
    Duration: 3.11 ms, Billed Duration: 4 ms, Memory Size: 512 MB, Max Memory Used: 7.57 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    null
    
    
    End of method: invoke