This article is not yet complete. If you have any questions, please feel free to ask in the Discussion.
Also, if you would like to contribute to this article, feel free to open a Pull Request on our GitHub Repository.
Deploy Go App
All projects based on Go Modules can be deployed. But you need to have a main.go
in the project root directory as the program compilation entry.
Custom Application Entry Point
If you have multiple entry points, you need to place them in the cmd
directory, such as a cmd/server-a/main.go
and a cmd/server-b/main.go
.
Zeabur will automatically recognize and deploy the application in the cmd
directory that matches the service name.
Alternatively, you can specify the entry point using the ZBPACK_GO_ENTRY
environment variable:
ZBPACK_GO_ENTRY=cmd/server-a/main.go
You can also set the go.entry
configuration in zbpack.json
:
{
"go": {
"entry": "cmd/server-a/main.go"
}
}
Setting Pre-build Commands
If your Go project requires running commands like go generate
before building, you can set them using the ZBPACK_BUILD_COMMAND
environment variable.
ZBPACK_BUILD_COMMAND=go generate ./...
You can also set the build_command
in zbpack.json
:
{
"build_command": "go generate ./..."
}
Your pre-build commands will run after dependencies are installed and before the build process.
Enabling cgo
If your Go project needs to use cgo, you can set go.cgo
in zbpack.json
:
{
"go": {
"cgo": true
}
}
Or set the ZBPACK_GO_CGO
environment variable:
ZBPACK_GO_CGO=true
Zeabur will automatically enable cgo and install the necessary C/C++ toolchain.
Listen on Port
Zeabur will automatically set the PORT
environment variable for the Go program to listen on.
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.ListenAndServe(":"+port, nil)