92

Get Go-ing with Cloud Functions #cloud #gcf

 5 years ago
source link: https://www.tuicool.com/articles/hit/aYbU7v3
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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:

<!----><code _ngcontent-c18="">// function.go
</code><code _ngcontent-c18="">package function
</code><code _ngcontent-c18="">
</code><code _ngcontent-c18="">import "net/http"
</code><code _ngcontent-c18="">
</code><code _ngcontent-c18="">func F(w http.ResponseWriter, r *http.Request) {
</code><code _ngcontent-c18="">        w.Header().Set("Content-Type", "text/plain; charset=utf-8")
</code><code _ngcontent-c18="">        w.Write([]byte(r.Header.Get("X-Forwarded-For")))
</code><code _ngcontent-c18="">}</code>

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
<!----><code _ngcontent-c18="">// function.go
</code><code _ngcontent-c18="">
</code><code _ngcontent-c18="">// Package function includes an example of processing a GCS event.
</code><code _ngcontent-c18="">package function
</code><code _ngcontent-c18="">
</code><code _ngcontent-c18="">import (
</code><code _ngcontent-c18="">        "context"
</code><code _ngcontent-c18="">        "log"
</code><code _ngcontent-c18="">)
</code><code _ngcontent-c18="">
</code><code _ngcontent-c18="">// GCSEvent holds event data from a Google Cloud Storage Event.
</code><code _ngcontent-c18="">type GCSEvent struct {
</code><code _ngcontent-c18="">        Bucket      string `json:"bucket"`
</code><code _ngcontent-c18="">        Name        string `json:"name"`
</code><code _ngcontent-c18="">}
</code><code _ngcontent-c18="">
</code><code _ngcontent-c18="">func F(ctx context.Context, e GCSEvent) error {
</code><code _ngcontent-c18="">        log.Printf("Processing file: %s", e.Name)
</code><code _ngcontent-c18="">        return nil
</code><code _ngcontent-c18="">}</code>

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
<!----><code _ngcontent-c18="">export GO111MODULE=on
</code><code _ngcontent-c18="">go mod init example.com/myfunctions
</code><code _ngcontent-c18="">go get cloud.google.com/go/translate</code>

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
meeYjqu.jpg!web

Posted in:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK