Specify dependencies in Go

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

Specify dependencies with Go modules

To specify Cloud Run 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.

The Functions Framework is a required dependency for all functions. Although Cloud Run functions installs it on your behalf when the function is created, we recommend that you include it as an explicit dependency for clarity.

If your function relies on private dependencies, we recommend that you mirror functions-framework to your private registry. Include the mirrored functions-framework as a dependency to your function to avoid installing the package from the public internet.

Specify dependencies with a vendor directory

Cloud Run 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 Run 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:

    go.mod
    go.sum
    
    # Also ignore Git directories. Delete the following two lines if you want to
    # upload them.
    .git
    .gitignore
    

Use 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 preceding instructions to avoid potential conflicts between the go.mod file and the vendor directory.