Building a Go App on App Engine

This guide helps you get started with App Engine and become familiar with developing, deploying, and managing a Go app.

After you build your app, you can read other tutorials to learn how to integrate with other Google Cloud Platform services and add more features to your app.

Costs

There are no costs associated with running this guide. Running this sample app alone does not exceed your free quota.

Setting up your development environment

You can either use your local machine, and use tools you're already familiar with, or Cloud Shell. Cloud Shell has Cloud SDK already installed, your environment already set up, and many other features.

Local Machine

Install Go and set up the Cloud SDK:

  1. Download, install, and then initialize Cloud SDK:

    Download and Install Cloud SDK

    Cloud SDK includes the gcloud command-line tool that provides the interface for GCP and is used to perform common platform tasks to manage your development workflow and your GCP resources.

  2. Download and install Go 1.11 if you haven't already done so.

    Download and Install Go 1.11

  3. Install the gcloud App Engine Go component:

    gcloud components install app-engine-go
    

Cloud Shell

Launch Cloud Shell, which has all the tools you'll need pre-installed:

Open Cloud Shell

Creating a GCP project

A project is required to use Google Cloud Platform, and forms the basis for using all GCP services.

  1. Create a new project by entering the following command in your development environment:

    gcloud projects create PROJECT_ID

  2. Create your App Engine app and its associated resources. You must choose a location, which cannot be changed later.

    gcloud app create

  3. Make sure that billing is enabled for your app to deploy on App Engine. Running this sample app alone does not exceed your free quota:

    Learn how to enable billing

Writing a basic web service for App Engine

Learn how to write a web service and declare runtime settings.

Structuring your files

Your service will have the following file structure:

  • go-app/: directory for your Go 1.11 service.
    • app.yaml: Your service's configuration settings.
    • main.go: Your application code.

Creating the app.yaml file

Every App Engine project has an app.yaml configuration file which specifies your service's runtime environment settings. Your service will not deploy without this file.

  1. Create a new folder called go-app for your Go 1.11 service:

    mkdir go-app

  2. In your go-app/ folder, create a file called app.yaml, and add the following contents:

    runtime: go111
    

    This is the simplest configuration for an App Engine app. It indicates to App Engine that you're using the Go 1.11 runtime. The app.yaml file can also specify network settings, scaling settings, and more. For more information, see the app.yaml reference.

Creating the main.go file

This sample uses the net/http package to create an HTTP server that prints "Hello world".

To set up your main.go file:

  1. In your go-app/ folder, create a main.go file.

  2. Add the package main statement to treat your code as an executable program:

    package main
    

    To successfully deploy a service in the Go 1.11 runtime, a package main statement must be defined at the beginning of at least one of your Go source files in the same directory as your service's app.yaml file.

  3. Import the following packages:

    import (
    	"fmt"
    	"log"
    	"net/http"
    	"os"
    )
    

  4. Define your HTTP handler:

    // indexHandler responds to requests with our greeting.
    func indexHandler(w http.ResponseWriter, r *http.Request) {
    	if r.URL.Path != "/" {
    		http.NotFound(w, r)
    		return
    	}
    	fmt.Fprint(w, "Hello, World!")
    }
    

    The http.ResponseWriter object assembles the HTTP server response; by writing to it, you send data to the browser. The http.Request object is a data structure that represents the incoming HTTP request.

  5. Register your HTTP handler:

    func main() {
    	http.HandleFunc("/", indexHandler)
    
    	port := os.Getenv("PORT")
    	if port == "" {
    		port = "8080"
    		log.Printf("Defaulting to port %s", port)
    	}
    
    	log.Printf("Listening on port %s", port)
    	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
    }
    

    The main function is the entry point of your executable program, so it starts the application. It begins with a call to the http.HandleFunc function which tells the http package to handle all requests to the web root ("/") with the indexHandler function.

    If the PORT environment variable is not set, port 8080 is used as a default. When your app is running on App Engine, the PORT environment variable is set for you, but when testing your app locally, you can set PORT to any preferred value.

Deploying your web service on App Engine

  1. In your go-app directory where your app.yaml file is located, deploy your web service to App Engine using the following command:

    gcloud app deploy

  2. To launch your browser and view your web service at http://YOUR_PROJECT_ID.appspot.com, run the following command:

    gcloud app browse

Congratulations! You've just created and deployed a service on App Engine.

Services and versions

The first service you deploy for your application will be the default service. You can specify the name of your service in the app.yaml file, but if the name is omitted, it is treated as default. You can deploy multiple services other than the default service.

You can update your service at any time by running the gcloud app deploy command. Each time you deploy, a new version is created and traffic is automatically routed to the latest version.

To confirm service creation and version deployment:

  1. View your App Engine services in the GCP Console:

    View services

    You should see one service listed, named default. The default service is publicly accessible at the following URL:

    https://YOUR_PROJECT_ID.appspot.com
    

  2. View your versions:

    View versions

    You should see one timestamped version listed, corresponding to your deployment.

To learn how to send requests to specific services and versions, see How Requests are Routed.

Next steps

Congratulations! You just set up and deployed your web application to App Engine.

Learn how to add other features to your application by exploring the following pages:

Was this page helpful? Let us know how we did:

Send feedback about...

App Engine standard environment for Go 1.11 docs