Application Development

Get Go-ing with Cloud Functions: Go 1.11 is now a supported language

Google Cloud Xively

Since releasing Node.js and Python as supported languages for Google Cloud Functions this summer, our team has been hard at work. Today, we're excited to announce support for Go on Cloud Functions.

Now in beta, the runtime uses the latest version, Go 1.11, which includes new language features like modules for integrating third-party dependencies into your code. Starting today, you can write Go functions that scale dynamically in response to load and integrate seamlessly with Google Cloud events.

Two ways to Go

Let’s take a look at the two types of functions that you can use with Cloud Functions: HTTP functions and background functions.

HTTP functions are what their name suggests: functions that are invoked by HTTP requests. They follow the http.HandlerFunc type from the standard library. For example, an HTTP function that returns a caller’s IP address might look like this:

  // function.go
package function

import "net/http"

func F(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "text/plain; charset=utf-8")
        w.Write([]byte(r.Header.Get("X-Forwarded-For")))
}

HTTP functions can be reached without an additional API gateway layer—Cloud Functions gives you an HTTPS URL. After the function is deployed, you can invoke the function by entering the URL into your browser.


In contrast, background functions are triggered in response to an event. Your function might, for example, run every time the contents of a Cloud Storage bucket change. Let’s look at an example function that triggers when a file is added to a Cloud Storage bucket:
  // function.go

// Package function includes an example of processing a GCS event.
package function

import (
        "context"
        "log"
)

// GCSEvent holds event data from a Google Cloud Storage Event.
type GCSEvent struct {
        Bucket      string `json:"bucket"`
        Name        string `json:"name"`
}

func F(ctx context.Context, e GCSEvent) error {
        log.Printf("Processing file: %s", e.Name)
        return nil
}

When you deploy your function, you also indicate a specific Cloud Storage bucket. Every time a new file appears in that bucket, your function runs and logs the file's name.


These are just two small examples of what’s possible with Cloud Functions and Go. The runtime supports a rich ecosystem of Go packages via Go modules. For example, to use the Cloud Translation client library in your function, create a `go.mod` file by running the following commands:
  export GO111MODULE=on
go mod init example.com/myfunctions
go get cloud.google.com/go/translate

When you deploy your function, Cloud Functions fetches and installs the dependencies listed in your `go.mod` file.


Using Cloud Functions, you can build serverless application backends, real-time data processing pipelines, chatbots, and video or image analysis tools, just to name a few. Now, you can also use the familiar building blocks of Go to build out your Cloud Functions.


We're continuously making improvements to Cloud Functions. For instance, environment variables are now generally available and we added new IAM security controls. We hope you're excited to try the Go runtime. To get started, check out the Go quickstart.
Cloud Function Youtube video