zesty.io

Product

Use Cases

Integrations

Learn

Not Categorized

Writing Google Cloud Functions in Go

Let’s try building and deploying a Google Cloud Function using Go!

Google Cloud recently added the Go language to its list of runtimes for the Cloud Functions serverless compute platform. The Go runtime uses version 1.11 and is currently in beta but available to everyone. If it follows the path of the Python and Node.js 8 runtimes I expect it to spend a short time in beta before being moved to general availability.

In this article I’ll take a a quick look at how to write and deploy a simple function in Go.

Coding

Let’s use the Go runtime to host a HTTP function that performs the same task as my Node.js 8 Cloud Functions demo does (or if you prefer Python 3 take a look at this example). I’ll use the randomuser.me API to output a JSON object representing data about a single user, then add one extra key named generator

I’m using the HTTP function model here (as opposed to a background function that could be triggered by an event such as a pub/sub message or a change in a file in a Google Cloud Storage bucket). In this model we’re expected to provide a function that can take two parameters: a http.ResponseWriter and a pointer to a http.Request.

On deployment the function will be bound to a URL. When something requests that URL the function is invoked. It should do whatever work it needs to before returning something back through the http.ResponseWriter.

In this example the body of the function grabs a JSON document from randomuser.me, unmarshals it then gets the first object from the “results” array. I add an extra key whose name is generator and set its value to google-cloud-function.

I won’t go into the details of handling arbitrary JSON in Go here. If you’re interested then I suggest you read this article which helped me.

Deployment

When deploying this function Google needs to know that it is intended for the Go runtime. To do this I need to install the latest gcloud beta commands:

$ gcloud components update
$ gcloud components install beta

Deployment is simple (use the beta commands and explicitly specify the Go runtime):

$ gcloud beta functions deploy getUserDetails --runtime go111 --entry-point F --trigger-http --project <projectId>

Where <projectId> is the ID of my Google Cloud project. The function can be invoked by simply visiting its URL which will look something like:

https://&lt;region&gt;-&lt;projectId&gt;.cloudfunctions.net/getUserDetails

<region> and <projectId> will depend on the Google Cloud project setup. The gcloud command displays the full invocation URL for you at the end of a successful deployment.

The output looks like this:

Sample output from Google Cloud Function using GoRuntime

The Cloud Functions console also shows that the function is using the Go runtime:

And that’s all there is to it!

. . .

If you’d like to use or study the code from this article feel free: I’ve </svg>“); background-size: 1px 1px; background-position: 0px calc(1em + 1px);" href="https://github.com/simonprickett/google-cloud-functions-go" target="_blank">put it on GitHub for you. Google’s announcement of beta support for Go in Cloud Functions can be read </svg>“); background-size: 1px 1px; background-position: 0px calc(1em + 1px);" href="https://cloud.google.com/blog/products/application-development/cloud-functions-go-1-11-is-now-a-supported-language" target="_blank">here. The announcement covers other topics that may be of interest too: using library code, and background Cloud Function development in Go.

I spent time researching this topic while helping to build features for </svg>“); background-size: 1px 1px; background-position: 0px calc(1em + 1px);" href="https://zesty.io/" target="_blank">Zesty.io, a SaaS Content Management System running on Google Cloud. Thanks for reading, please give me a follow if you’d like to see more in future! I write about a range of technologies including Node.js, Go, Python, Raspberry Pi and Amazon Alexa.

. . .

This article was originally published on Simon’s Mediumand syndicated on ITNext.

By Simon Prickett

Software professional, specializing in IoT, Node, mobile web & apps. Atheist, electronics hobbyist. Also aviation, cars & hockey.

Related Articles

Subscribe to the zestiest newsletter in the industry

Get the latest from the Zesty team, from whitepapers to product updates.