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.

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 cgo in zbpack.json:

{
    "cgo": true
}

Or set the CGO_ENABLED environment variable:

CGO_ENABLED=1

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)