Go is a statically typed and compiled programming language. If you use Go, you must compile your program on an on-premises machine and package your code in the ZIP format. This topic describes how to package the Function Compute SDK for Go library together with your code.
Prerequisites
The Go programming language is installed. Function Compute supports Go 1.x. We recommend that you use Go 1.8 or later.
Compile and package code on Linux or macOS
Download the Function Compute SDK for Go library.
go get github.com/aliyun/fc-runtime-go-sdk/fc
In the directory in which the file is stored, run the following command to compile the file:
GOOS=linux go build main.go
NoteThe
main.go
file is used as an example. Replace it with the name of the actual file.After the file is compiled, a binary file is generated whose name is the same as that of the file in the directory.
Add
GOOS=linux
to the command to ensure that the compiled executable file is compatible with the Go runtime of Function Compute. Pay special attention to this when you compile your files in a non-Linux environment.Note:
For Linux, we recommend that you add
CGO_ENABLED=0
to the command to use pure static compilation. This way, the executable files do not require external dependencies such as the libc library. This prevents incompatibility between the compilation environment and the dependencies of the Go runtime. Example:GOOS=linux CGO_ENABLED=0 go build main.go
If your machine uses a computer with macOS and M1 chips or another ARM architecture, add
GOARCH=amd64
to the command to compile files across platforms. Example:GOOS=linux GOARCH=amd64 go build main.go
Package the binary file that is generated in the preceding step.
zip fc-golang-demo.zip main
Compile and package files on Windows
Compile the executable file.
Press Win+R to open the Run dialog box.
Enter cmd and press Enter.
In the command prompt window, run the following commands:
set GOOS=linux set GOARCH=amd64 go build -o main main.go
Use the build-fc-zip tool to package files.
Use the go get method from GitHub to download the build-fc-zip tool.
go get -u github.com/aliyun/fc-runtime-go-sdk/cmd/build-fc-zip
Use the build-fc-zip tool to package files. If you use the default installation method for Go, the tool is installed in the %USERPROFILE%\go\bin directory.
%USERPROFILE%\go\bin\build-fc-zip.exe -output main.zip main
Configure Function Compute handlers
Create a function in the service and set Runtime to Go 1.
Go is a compiled language. You need to upload the executable binary file as a ZIP package after the file is compiled on an on-premises machine. In the Handler settings of the function for Go in the Function Compute console, you need to set the Handler parameter of the function to
[File name]
. The [File name] variable refers to the name of the compiled binary file. When the function is invoked, Function Compute directly executes the binary file.If the compiled binary file is stored in the root directory of the ZIP package, as shown in the following figure, set the Handler parameter to
main
.If the compiled binary file is not stored in the root directory of the ZIP package, but stored in the bin/ directory, as shown in the following figure, set the Handler parameter to
bin/main
.
For more information about other deployment methods, see the following topics: