Stay organized with collections Save and categorize content based on your preferences.

Specifying dependencies in Go

Cloud Functions in Go must provide all of their dependencies via either Go modules with a go.mod file, or a vendor directory. Your function cannot specify dependencies using both Go modules and a vendor directory at the same time.

The Go runtime includes a number of system packages in the execution environment. If your function uses a dependency that requires a system package that is not listed, you can request a package.

The content in this document applies to both Go 1.11 and Go 1.13.

Using Go modules

Cloud Functions support Go's experimental Modules functionality, which enables you to specify dependencies in a go.mod file at the root of your project. When you deploy your function, dependencies specified in the go.mod file will be fetched and built automatically.

The behavior of Go modules differs depending on whether you are developing inside or outside of GOPATH. To determine whether you are inside GOPATH:

  1. Navigate to your project directory.

  2. Find your GOPATH by running the command:

    go env GOPATH

    This outputs a line similar to:

  3. Find your current working directory by running:


If your working directory begins with YOUR_GOPATH, you are inside GOPATH. In this case, generate your go.mod file by running the following three commands:

export GO111MODULE=on
go mod init
go mod tidy

If your working directory does not begin with YOUR_GOPATH, you are outside GOPATH. In this case, generate your go.mod file by running the commands:

go mod init MODULE
go mod tidy

In the above example, MODULE is the name of your module. For example, your module name might be (note that the domain name is required). The go command automatically detects the module name when you are inside GOPATH.

After you've created a go.mod file, you can use the go get command to fetch dependencies and automatically add them to your project. For example:


In the above example, DEPENDENCY is a dependency that you want to add to your function. For example, the command go get adds the Cloud Storage client library to your function.

Using a vendor directory

Cloud Functions also allows you to include your dependencies via a vendor directory. Most of the time, vendor directories are maintained with a dependency manager. You can use any dependency manager you like. For example, you can use Go's Modules functionality to create a vendor directory from your go.mod file.

Using a vendor directory is helpful if your dependency is not available via a dependency manager or if your Cloud Functions environment's internet access is restricted.

Once you have set up the vendor directory, you may need to perform additional steps to ensure that its contents are respected, depending on which version of Go you are using:

Go version 1.16 or higher

If you are using Go 1.16 or higher, you can leave the vendor directory as-is. When the main module contains a top-level vendor directory and its go.mod file specifies Go 1.16 or higher, the go command defaults to -mod=vendor for operations that accept that flag.

Go version older than 1.16

If you are using a version of Go older than 1.16, you must do the following for the contents of the vendor directory to be respected.

For versions of Go older than 1.16, if you have a go.mod file and a vendor directory, the vendor directory will be ignored when you deploy your function. You can use a .gcloudignore file to avoid uploading your go.mod and go.sum files:

  1. Create a .gcloudignore file at the root of your project directory with the following contents:

    # Also ignore Git directories. Delete the following two lines if you want to
    # upload them.
  2. Create a vendor directory using the contents of your go.mod file by running the following command:

    go mod vendor

When you use the Google Cloud CLI to deploy your function, you can ensure that go.mod is not uploaded by using .gcloudignore.

Using private dependencies

If your function's dependencies are hosted in a repository that is not publicly accessible, you must use a vendor directory to fetch your dependencies before deploying your function. If you plan to use a go.mod file, see the instructions above to avoid potential conflicts between the go.mod file and the vendor directory.