Quickstart for Go App Engine Standard Environment

This quickstart shows you how to create a small App Engine application that displays a short message.

Before you begin

Before running and deploying this quickstart, you must set up a Cloud Platform project for App Engine and then install the Cloud SDK:

  1. Create a new Cloud Platform project and App Engine application using the Cloud Platform Console:

    Go to App Engine

    When prompted, select the region where you want your App Engine application located. After your App Engine application is created, the Dashboard opens.

  2. Download and install the Google Cloud SDK and then initialize the gcloud tool:

    Download the SDK

App Engine locations

App Engine is regional, which means the infrastructure that runs your apps is located in a specific region and is managed by Google to be redundantly available across all the zones within that region.

Meeting your latency, availability, or durability requirements are primary factors for selecting the region where your apps are run. You can generally select the region nearest to your app's users but you should consider the location of the other Cloud Platform products and services that are utilized by your app. Using services across multiple locations can affect your app's latency as well as pricing.

App Engine is available in the following regions:

  • us-central1
  • us-east1
  • us-east4
  • europe-west1
  • europe-west2
  • europe-west3
  • asia-northeast1
  • australia-southeast1

Download the Hello World app

We've created a simple Hello World app for Go so that you can quickly get a feel for deploying an app to Google Cloud Platform.

To download Hello World app to your local machine:

  1. Clone the Hello World sample app repository to a directory called helloworld on your local machine:

    git clone -b part1-helloworld https://github.com/GoogleCloudPlatform/appengine-guestbook-go.git helloworld
    
  2. Navigate to that helloworld directory:

    cd helloworld
    

Alternatively, you can download the sample as a .zip file and then extract it to a directory called helloworld.

Test the application

Test the application using the local development server, which is included with the SDK.

  1. From within the helloworld directory where the app's app.yaml configuration file is located, run the dev_appserver.py command to compile your app and then start the local development server:

      dev_appserver.py app.yaml
    

    The local development server is now running and listening for requests on port 8080. Something go wrong?

  2. Visit http://localhost:8080/ in your web browser to see the app in action.

Running the local development server (dev_appserver.py)

To run the local development server, you can either run dev_appserver.py by specifying the full directory path or you can add dev_appserver.py to your PATH environment variable:

  • If you installed the original App Engine SDK, the tool is located at:

    [PATH_TO_APP_ENGINE_SDK]/dev_appserver.py
    
  • If you installed the Google Cloud SDK, the tool is located at:

    [PATH_TO_CLOUD_SDK]/google-cloud-sdk/bin/dev_appserver.py
    

    Tip: To add the Google Cloud SDK tools to your PATH environment variable and enable command-completion in your shell, you can run:

    [PATH_TO_CLOUD_SDK]/google-cloud-sdk/install.sh
    

For more information about running the local development server including how to change the port number, see the Local Development Server reference.

Make a change

The local development server watches for changes in your project files. As you update your source code, your app is recompiled and launched.

  1. Try it now: Leave the local development server running and then try editing the hello.go file to change Hello, world! to something else.
  2. Reload http://localhost:8080/ to see the change.

Deploy your app

To deploy your app to App Engine, run the following command from within the root directory of your application where the app.yaml file is located:

gcloud app deploy

Learn about the optional flags.

Common gcloud command flags

  • Include the --version flag to specify an ID that uniquely identifies that version of your app, otherwise one is generated for you. Example: --version [YOUR_VERSION_ID]
  • Include the --project flag to specify an alternate Cloud Platform project ID to what you initialized as the default in the gcloud tool. Example: --project [YOUR_PROJECT_ID]

Example:

gcloud app deploy --version pre-prod-5 --project my-sample-app

To learn more about deploying your app from the command line, see Deploying a Go App . For a list of all the command flags, see the gcloud app deploy reference.

View your application

To launch your browser and view the app at http://[YOUR_PROJECT_ID].appspot.com, run the following command:

gcloud app browse

Congratulations!

You have completed this quickstart.

See the following sections for information about cleaning up as well as links to the possible next steps that you can take.

To learn more about this Hello World app, see the Hello World code review section.

Clean up

To avoid incurring charges, you can delete your Cloud Platform project to stop billing for all the resources used within that project.

  1. In the Cloud Platform Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click
      Delete project
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

What's next

Hello World code review

Hello World is the simplest possible App Engine app: it contains only one service, has only one version, and all of the code is located within the app's root directory. This section describes each of the app files in detail.

This Go package responds to any request by sending a response containing the message Hello, world!.

package hello

import (
    "fmt"
    "net/http"
)

func init() {
    http.HandleFunc("/", handler)
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello, world!")
}

app.yaml

The app.yaml configuration file tells the App Engine service which runtime to use and which URLs should be handled by our Go program.

runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app

From top to bottom, this configuration file says the following about this application:

  • This code runs in the go runtime environment, with API version go1.
  • Every request to a URL whose path matches the regular expression /.* (all URLs) should be handled by the Go program. The _go_app value is a magic string recognized by the local development server; it is ignored by the production App Engine servers.

The syntax of this file is YAML. For a complete list of configuration options, see the Go Application Configuration page.

Learn the whole platform

Now that you know what it's like to develop and deploy App Engine apps, you can stretch out and see the rest of Google Cloud Platform. For a guided walkthrough which teaches you how to create an application that uses more of the Cloud platform, not just App Engine, check out our "Creating a Guestbook" tutorial. The tutorial builds on what you've learned from this quickstart, teaching you how to deploy a full-fledged Guestbook application that lets authenticated Google accounts post messages to a public page.

Learn more about App Engine standard environment

Here are some topics to help you to continue learning about App Engine:

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...

App Engine standard environment for Go