Go
Railpack builds and deploys Go applications with static binary compilation.
Detection
Section titled “Detection”Your project will be detected as a Go application if any of these conditions are met:
- A
go.modfile exists in the root directory - A
go.workfile exists in the root directory (Go workspaces) - A
main.gofile exists in the root directory
Versions
Section titled “Versions”The Go version is determined in the following order:
- Read from the
go.modfile - Set via the
RAILPACK_GO_VERSIONenvironment variable - Defaults to
1.23
Configuration
Section titled “Configuration”Railpack builds your Go application as a static binary by default. The build process:
- Installs Go dependencies
- Builds your application with optimized flags (
-ldflags="-w -s") - Names the output binary
out
Railpack determines the main package to build in the following order:
- The module specified by the
RAILPACK_GO_WORKSPACE_MODULEenvironment variable (for workspaces) - The package specified by the
RAILPACK_GO_BINenvironment variable - The root directory if it contains Go files
- The first subdirectory in the
cmd/directory - For workspaces: the first module containing a
main.gofile - The
main.gofile in the root directory
Config Variables
Section titled “Config Variables”| Variable | Description | Example |
|---|---|---|
RAILPACK_GO_VERSION | Override the Go version | 1.22 |
RAILPACK_GO_BIN | Specify which command in cmd/ to build | server |
RAILPACK_GO_WORKSPACE_MODULE | Specify which workspace module to build | api |
CGO_ENABLED | Enable CGO for non-static binary compilation | 1 |
Go Workspaces
Section titled “Go Workspaces”Railpack supports Go workspaces (introduced in Go 1.18) for multi-module projects:
- Detects projects with a
go.workfile at the root - Automatically discovers and copies all module dependencies
- Builds the first module with a
main.gofile by default - Use
RAILPACK_GO_WORKSPACE_MODULEto specify which module to build
Example workspace structure:
├── go.work├── api/│ ├── go.mod│ └── main.go└── shared/ ├── go.mod └── lib.goTo build a specific module:
RAILPACK_GO_WORKSPACE_MODULE=apiCGO Support
Section titled “CGO Support”By default, Railpack builds static binaries with CGO_ENABLED=0. If you need
CGO support:
- Set the
CGO_ENABLEDenvironment variable to1 - Railpack will include the necessary build dependencies (gcc, g++, libc6-dev)
- The runtime image will include libc6 for dynamic linking