Stay organized with collections
Save and categorize content based on your preferences.
Specifying the Go Version
By default the Go buildpack uses the latest stable version of the Go compiler. If your application requires a specific version, you can use the GOOGLE_GO_VERSION environment variable to provide a semver constraint that will be used to select an available Go version.
The Go buildpack compiles your application source into an executable using the go build command. The following environment variables can be used to configure the build behavior:
GOOGLE_BUILDABLE
Specifies path to a buildable unit.
Example: ./maindir for Go will build the package rooted at maindir.
GOOGLE_CLEAR_SOURCE
Omit the source code from the application image. If the application depends on static files, such as Go templates, setting this variable may cause the application to misbehave.
Example: true, True, 1 will clear the source.
GOOGLE_GOGCFLAGS
Passed to go build and go run as -gcflags value with no interpretation.
Example: all=-N -l enables race condition analysis and changes how source filepaths are recorded in the binary.
GOOGLE_GOLDFLAGS
Passed to go build and go run as -ldflags value with no interpretation.
Example: -s -w is used to strip and reduce binary size.
Managing Dependencies
We recommend that you use Go modules to manage dependencies in your Go app. The Go buildpack uses the go build command to build your app and therefore matches the behavior of Go itself. To ensure that your app uses module-aware mode, you should include a go.mod file in your application root.
Vendoring dependencies
Vendoring copies the packages your app uses into the application directory instead of downloading modules from their sources during the build process. Go provides the go build command to vendor the packages your app needs into a directory named vendor in your app's root directory.
Configure the Application Entrypoint
By default, the Go buildpack will configure the application container entry invoke the main executable produced when compiling your application source. If you need to override this, you can do so by providing a Procfile or passing the GOOGLE_ENTRYPOINT environment variable.
Environment Variables
The Go buildpack supports the following environment variables to customize your container
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-25 UTC."],[[["\u003cp\u003eThe Go buildpack defaults to the latest stable Go version, but you can specify a different version using the \u003ccode\u003eGOOGLE_GO_VERSION\u003c/code\u003e environment variable with a semver constraint.\u003c/p\u003e\n"],["\u003cp\u003eCompilation behavior can be configured via environment variables like \u003ccode\u003eGOOGLE_BUILDABLE\u003c/code\u003e to specify a buildable path, \u003ccode\u003eGOOGLE_CLEAR_SOURCE\u003c/code\u003e to omit source code, and \u003ccode\u003eGOOGLE_GOGCFLAGS\u003c/code\u003e/\u003ccode\u003eGOOGLE_GOLDFLAGS\u003c/code\u003e to pass custom flags.\u003c/p\u003e\n"],["\u003cp\u003eThe buildpack utilizes Go modules for dependency management, recommending the inclusion of a \u003ccode\u003ego.mod\u003c/code\u003e file in your application's root directory for module-aware mode.\u003c/p\u003e\n"],["\u003cp\u003eVendoring dependencies is supported by including a \u003ccode\u003evendor\u003c/code\u003e directory in the app's root, using the \u003ccode\u003ego build\u003c/code\u003e command to collect dependencies.\u003c/p\u003e\n"],["\u003cp\u003eYou can override the default application entrypoint (the compiled \u003ccode\u003emain\u003c/code\u003e executable) by providing a \u003ccode\u003eProcfile\u003c/code\u003e or using the \u003ccode\u003eGOOGLE_ENTRYPOINT\u003c/code\u003e environment variable.\u003c/p\u003e\n"]]],[],null,["# Building a Go application\n\nSpecifying the Go Version\n-------------------------\n\nBy default the Go buildpack uses the latest stable version of the Go compiler. If your application requires a specific version, you can use the `GOOGLE_GO_VERSION` environment variable to provide a semver constraint that will be used to select an available Go version. \n\n pack build sample-go --builder=gcr.io/buildpacks/builder \\\n --env GOOGLE_GO_VERSION=\"17.x.x\"\n\nCompilation Flags\n-----------------\n\nThe Go buildpack compiles your application source into an executable using the `go build command`. The following environment variables can be used to configure the build behavior:\n\n- `GOOGLE_BUILDABLE`\n Specifies path to a buildable unit.\n Example: `./maindir` for Go will build the package rooted at `maindir`.\n\n- `GOOGLE_CLEAR_SOURCE`\n Omit the source code from the application image. If the application depends on static files, such as Go templates, setting this variable may cause the application to misbehave.\n Example: `true`, `True`, `1` will clear the source.\n\n- `GOOGLE_GOGCFLAGS`\n Passed to `go build` and `go run` as `-gcflags` value with no interpretation.\n Example: `all=-N -l` enables race condition analysis and changes how source filepaths are recorded in the binary.\n\n- `GOOGLE_GOLDFLAGS`\n Passed to go build and go run as `-ldflags` value with no interpretation.\n Example: `-s -w` is used to strip and reduce binary size.\n\nManaging Dependencies\n---------------------\n\nWe recommend that you use [Go modules](https://go.dev/blog/using-go-modules) to manage dependencies in your Go app. The Go buildpack uses the `go build` command to build your app and therefore matches the behavior of Go itself. To ensure that your app uses module-aware mode, you should include a `go.mod` file in your application root.\n\n### Vendoring dependencies\n\nVendoring copies the packages your app uses into the application directory instead of downloading modules from their sources during the build process. Go provides the go build command [to vendor the packages your app needs](https://pkg.go.dev/cmd/go#hdr-Modules_and_vendoring) into a directory named `vendor` in your app's root directory.\n\nConfigure the Application Entrypoint\n------------------------------------\n\nBy default, the Go buildpack will configure the application container entry invoke the `main` executable produced when compiling your application source. If you need to override this, you can do so by providing a `Procfile` or passing the `GOOGLE_ENTRYPOINT` environment variable.\n\nEnvironment Variables\n---------------------\n\nThe Go buildpack supports the following environment variables to customize your container\n\n### GO\n\nSee Go documentation on [Environment Variables](https://pkg.go.dev/cmd/go#hdr-Environment_variables)\n\n**Example:** `GOFLAGS=-flag=value` passes `-flag=value` to `go` commands."]]