Go Functions Framework

Grant Timmerman
Nov 12, 2019 · 2 min read
Google Cloud Functions + Go (Logo by Takuya Ueda — src)

In this post, you’ll learn about the open-source Go Functions Framework that enables you to develop Golang Google Cloud Functions on your computer and deploy them to the cloud.


So why is the Go Functions Framework important? Well, it enables…

  • 💻 Local testing of your Google Cloud Functions in Go
  • 🔢 Transparent versioning of your application code on Google Cloud
  • 🌐 Write once, run anywhere: Cloud Functions, Cloud Run, other Clouds

Personally, I really like how I can easily use localhost to test my functions.

Before we begin, let’s first ensure we’re running on the same environment. Print your go version with this command:

go version

The output should be Go 1.11 or higher. If not, update Go by uninstalling and re-installing Go: https://golang.org/dl/

Then, let’s create a new directory for our function called hello:

mkdir -p hello/cmd
cd hello

Create a Go module:

go mod init example.com/hello

Create a function.go file with the following contents (any package name):

A simple “hello” package.

Create a folder cmd with a file cmd/main.go with the following contents for our main package:

Our main program that starts the Function Framework.

This file uses the Go Functions Framework to start our function.

Test your function locally using the following command:

go run cmd/main.go

Your Google Cloud Function is now running locally. You can curl your function to invoke your function:

curl localhost:8080

You’ll see the output:

Hello, Functions Framework!

Awesome!

To deploy to Google Cloud Functions, run the following command:

gcloud functions deploy HelloWorld --runtime go111 --trigger-http

After ~2 minutes, you’ll see the URL look something like this:

https://us-central1-my-project.cloudfunctions.net/HelloWorld

You can invoke the function by going to your URL.

From the command-line, this script will get the URL and curl your function:

curl $(gcloud functions describe HelloWorld --format 'value(httpsTrigger.url)')

Wicked! 😄

You just went from testing your Go function on your computer to deploying to Google Cloud which can theoretically scale to 100,000,000 reqs per 100 seconds (🔗).

If you want to follow updates, or learn more, check out the GitHub repo: https://github.com/GoogleCloudPlatform/functions-framework-go

You might also be interested in this blogpost and video:

Google Cloud - Community

A collection of technical articles published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Grant Timmerman

Written by

Google • Explorer

Google Cloud - Community

A collection of technical articles published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

More From Medium

More from Google Cloud - Community

More from Google Cloud - Community

More from Google Cloud - Community

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade