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

Prerequisites

Before you begin, make sure that you have completed the following operations:
  • Install Node.js 8.6.0 or later. For more information, visit Downloads.
  • Install Docker. For more information, visit Docker Hub.

Procedure

  1. Run the following command to clone the sample project to the on-premises machine:
    git clone https://github.com/awesome-fc/fc-custom-demo          
    Note If you do not install Git, you can download the sample project from https://github.com/awesome-fc/fc-custom-demo.
  2. Optional:Modify the sample code in the go-demo directory to implement your business logic.
  3. Run the following command to go to the cloned sample project:
    1. Run the following command to go to the go-demo directory:
      cd go-demo
  4. Run the following command to deploy the function:
    make deploy
    The following output is returned:
    docker build -t fc-go-runtime  -f build-image/Dockerfile build-image
    Sending build context to Docker daemon  3.072kB
    Step 1/5 : FROM golang:1.12.16-stretch
     ---> 7ad03a8aece5
    ...
    Step 5/5 : RUN go get github.com/awesome-fc/golang-runtime
     ---> Using cache
     ---> 262e7f38ac05
    Successfully built 262e7f38ac05
    Successfully tagged fc-go-runtime:latest
    docker run -it -v $(pwd):/tmp fc-go-runtime bash -c "go build -o /tmp/code//bootstrap /tmp/code/main.go"
    chmod +x code/bootstrap
    fun deploy -y
    using template: template.yml
    ···
    Waiting for service go_demo to be deployed...
            Waiting for function fc_go to be deployed...
                    Waiting for packaging function fc_go code...
                    The function fc_go has been packaged. A total of 1 file were compressed and the final size was 3.76 MB
            function fc_go deploy success
    service go_demo deploy success
  5. Run the following command to invoke the deployed function:
    fun invoke -e "Hello World"
    The following output is returned:
    ...
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: 4c1451b2-f29b-4554-87e5-126f3bc11fcf
    2020-04-07T02:53:01.981Z: 4c1451b2-f29b-4554-87e5-126f3bc11fcf [INFO]  hello golang!
    FC Invoke End RequestId: 4c1451b2-f29b-4554-87e5-126f3bc11fcf
    
    Duration: 1.03 ms, Billed Duration: 100 ms, Memory Size: 512 MB, Max Memory Used: 4.39 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    Hello World

References

For more information about how to migrate the Go application framework to Function Compute, see the following topics: