Specify dependencies

You can use any linux/amd64-compatible package with instances running in the App Engine flexible environment. These instructions assume you are using the go get command to get the packages directly from supported repositories, such as GitHub, Bitbucket, LaunchPad, and others.

Starting in Go version 1.22 and later:

  • You can't use go get outside of a module in the legacy GOPATH mode (GO111MODULE=off). For more information, see Tools.

  • Go recommends that you use a go.mod file for managing dependencies. To install dependencies during deployment, include a go.mod file in the same folder as the app.yaml file. For more information about Go versions, and managing dependencies for vendor directories, see GOPATH and Modules.

Declare and manage dependencies

Go applications are organized into packages that mirror the directory structure of your source files. When you use an import statement, the relative paths in the import are interpreted. Valid import paths are fully-qualified paths that are relative to the src subdirectory of all the directories that are specified in your GOPATH.

For example, consider an example app where you define the following:

  • GOPATH module:

     export GOPATH=/home/fred/go.
    
  • src1-1.go file:

    import "foo/bar"
    
  • The gcloud CLI looks for the foo/bar package in the /home/fred/go/src/foo/bar location when you run or deploy the app.

If you include your package sources in GOPATH, you must be careful not to place your source code within your app's directory where the app.yaml file is located. If that happens, subtle problems can occur because a package might get loaded twice, once for the path relative to a service's directory, and once again for the fully-qualified path. To avoid problems, the gcloud CLI will scan both your app's directory and GOPATH, and then report an error if a conflict is detected.

For best results, we recommend the following:

  • Create a separate directory in your app's directory for each service.
  • Each service's directory should contain the service's app.yaml file and one or more .go files.
  • Don't include any subdirectories in a service's directory.
  • Your GOPATH should specify a directory that is outside your app's directory and contain all the dependencies that your app imports.

Download required packages

You can use the go get command to download packages. For example, to download packagename from the GitHub my_repo:

go get github.com/my_repo/packagename

Deploy to App Engine

To deploy your application to App Engine, you must deploy the libraries that are required by your app along with your application code. For complete details, see Testing and deploying your application.