Specify dependencies in Go

You can specify your Cloud Function dependencies with either a Go Module or a vendor directory.

Specify dependencies with Go modules

To specify Cloud Functions dependencies with a Go Module, you list them in a go.mod file. When you deploy your function, Go automatically incorporates the dependencies in your go.mod file.

To create a go.mod file, see Managing dependencies in Go.

Specify dependencies with a vendor directory

Cloud Functions also lets you include your dependencies via a vendor directory. 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.

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.

You must include the Functions Framework for Go in your vendor directory. To use the Go toolchain to do this:

  1. Add the following directive to the import block of your Go code:

    _ "github.com/GoogleCloudPlatform/functions-framework-go/funcframework"
  2. Update your go.mod file to include the new imported package:

    go mod tidy
  3. Create a vendor directory using the contents of your go.mod file:

    go mod vendor

Go versions earlier than 1.16

For versions of Go earlier 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. To ensure that your vendor directory is respected, 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.

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.