The Go runtime

The Go language is quickly gaining popularity for its simplistic design and performance. Google Cloud offers support for Go in many products and services, which is no surprise given that Go was created at Google. With fast startup times and low overhead, Go is a natural fit for App Engine services. The App Engine standard environment offers runtime for Go 1.6 and 1.8.

Much like Python, Google offers a number of tools and libraries for developing Go services and integrating with other Google products. In order to develop App Engine services in Go, developers will need to install the Go 1.6 or 1.8 runtime, as well as the Google Cloud App Engine Go component. The App Engine Go component can be installed by running the following command:

gcloud components install app-engine-go

As we've seen in developing App Engine services in Python, we'll need to define a app.yaml configuration file along with any source code. Also similar to Python, the sand-boxed nature of the App Engine standard environment means some components of Go are limited. This includes tasks like parallel processing and writing to the underlying filesystem. Note that goroutines are supported, but all goroutines are executed on a single system thread.

For building on our Python example, a simple Go implementation of the colors service is available in the book's source code in chapter_04/example_02. Fundamentally, this example looks very similar to our Python application. The app.yaml configuration file is relatively minimal:

  • The app.yaml file is as follows:
service: colors
runtime: go
api_version: go1.8

handlers:
- url: /.*
script: _go_app

There are two notable changes from the configuration file we used in our Python service. First, we've included service: colors. This informs App Engine that this will be a separate service from the default, called colors. Second, our script doesn't refer to an actual script. For the Go runtime, we specify our script as _go_app. This informs App Engine that requests to the given URL should be delegated to our Go application.

Whereas Python services must conform to WSGI or CGI, the Go runtime works by leveraging Go's core net/http library. Developers create HTTP handlers which are then invoked by App Engine based on route handlers defined in the application configuration file. For our colors service, the /colors implementation is relatively straightforward:

  • The colors.go file is as follows:
package colors
...
func init() {
...
http.HandleFunc("/colors", handler)
}

func handler(w http.ResponseWriter, r *http.Request) {
response := colorResponse{"blue", "Go 1.8 ...", instanceId}
body, err := json.Marshal(response)

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("content-type", "application/json")
w.Write(body)
}

Note the lack of a main method and server definition. The App Engine Go runtime will provide these for your service at runtime, allowing developers to focus on the core business logic.

As with our Python service, we can run the colors service using the App Engine development server by executing dev_appserver.py app.yaml from within the example_02 directory. To see what a colors response looks like, visit http://localhost:8080/colors.