Region ID
The REGION_ID
is an abbreviated code that Google assigns
based on the region you select when you create your app. The code does not
correspond to a country or province, even though some region IDs may appear
similar to commonly used country and province codes. For apps created after
February 2020, REGION_ID.r
is included in
App Engine URLs. For existing apps created before this date, the
region ID is optional in the URL.
Learn more about region IDs.
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 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.
Set 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 the Google Cloud CLI already installed, your environment already set up, and many other features.
Local Machine
Install Go and set up the gcloud CLI as described in Setting up your development environment.
Cloud Shell
Launch Cloud Shell, which has all the tools you'll need pre-installed:
Create a Google Cloud project
A project is required to use Google Cloud, and forms the basis for using all Google Cloud services.
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build API.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build API.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- Create your App Engine app and its associated resources. You must
choose a location, which cannot be changed later.
gcloud app create
Write a basic web service for App Engine
Learn how to write a web service and declare runtime settings.
Structure your files
Your service will have the following file structure:
go-app/
: directory for your Go service.app.yaml
: Your service's configuration settings.main.go
: Your application code.
Create 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.
Create a new folder called
go-app
for your Go service:mkdir go-app
In your
go-app/
folder, create a file calledapp.yaml
, and add the following contents:runtime: go123 # or another supported version
This is the simplest configuration for an App Engine app. It indicates to App Engine that you're using Go. The
app.yaml
file can specify other Go versions, network settings, scaling settings, and more. For more information, see theapp.yaml
reference.
Create 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:
In your
go-app/
folder, create amain.go
file.Add the
package main
statement to treat your code as an executable program:package main
To successfully deploy a service, a
package main
statement must be defined at the beginning of at least one of your Go source files.Import the following packages:
Define your HTTP handler:
The
http.ResponseWriter
object assembles the HTTP server response; by writing to it, you send data to the browser. Thehttp.Request
object is a data structure that represents the incoming HTTP request.Register your HTTP handler:
The
main
function is the entry point of your executable program, so it starts the application. It begins with a call to thehttp.HandleFunc
function which tells thehttp
package to handle all requests to the web root ("/"
) with theindexHandler
function.If the
PORT
environment variable is not set, port8080
is used as a default. When your app is running on App Engine, thePORT
environment variable is set for you, but when testing your app locally, you can setPORT
to any preferred value.
Deploy your web service on App Engine
In your
go-app
directory where yourapp.yaml
file is located, deploy your web service to App Engine using the following command:gcloud app deploy
To launch your browser and view your web service at
https://PROJECT_ID.REGION_ID.r.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:
View your App Engine services in the Google Cloud console:
You should see one service listed, named
default
. The default service is publicly accessible at the following URL:https://PROJECT_ID.REGION_ID.r.appspot.com
View your 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:
- Learn more about writing application logs and interpreting system logs in Reading and Writing Application Logs.
- For a deeper dive into how App Engine works, see the Go Runtime Environment.
- Read about request handling and request routing.