Go App Engine applications communicate with the outside world via a web server compatible with Go's http package. This makes writing Go App Engine applications very similar to writing stand-alone Go web applications.
This tutorial creates a tiny application that displays a short message.
Download the App Engine SDK for Go
Before getting started, be sure to download the Go SDK for App Engine.
Creating a simple HTTP handler
Create a directory named
myapp. All files for this application reside in this
myapp directory, create a file named
hello.go, and give it the
This Go package responds to any request by sending a response containing the message
Creating the configuration file
An App Engine application has a configuration file called
other things, this file tells the App Engine service which runtime to use and
which URLs should be handled by our Go program.
myapp directory, create a file named
app.yaml with the following contents:
From top to bottom, this configuration file says the following about this application:
- The application identifier is
helloworld. When you register your application with App Engine later in this tutorial, you will select a unique identifier, and update this value. This value can be anything during development. For now, leave it set to
- This is version number
1of this application's code. If you adjust this before uploading new versions of your application software, App Engine will retain previous versions, and let you roll back to a previous version using the administrative console.
- This code runs in the
goruntime environment, with API version
- Every request to a URL whose path matches the regular expression
/.*(all URLs) should be handled by the Go program. The
_go_appvalue is a magic string recognized by the development web server; it is ignored by the production App Engine servers.
Testing the application
hello package and configuration file mapping every URL to the Go program, the application is complete. You can now test it with the web server included with the App Engine SDK.
Check that you have everything in its right place. The application's directory structure should look like this:
myapp/ app.yaml hello.go
Run the following command, giving it the path to the
myapp directory, to compile your app and start the development web server:
/path/to/go_appengine/goapp serve myapp/
You may drop the
/path/to/go_appengine/ if you added it to your
PATH, as suggested earlier. You can also omit the path altogether if
myapp is your current directory, so the command is simply:
The web server is now running, listening for requests on port 8080. Test the application by visiting the following URL in your web browser:
For more information about running the development web server, including how to change which port it uses, see the Go Development Server reference, or run
goapp help serve.
The development app server knows to watch for changes in your file. As you update your source, it recompiles them and relaunches your local app. There's no need to restart
Try it now: leave the web server running, then edit
hello.go to change
Hello, world! to something else. Reload http://localhost:8080/ to see the change.
To shut down the web server, make sure the terminal window is active, then press Control-C (or the appropriate "break" key for your console).
Leave the web server running for the rest of this tutorial. If you need to stop it, you can restart it again by running the command above.
Uploading the application
- Sign in to App Engine using your Google account. If you do not have a Google account, you can create a Google account with an email address and password.
If you haven't already done so, create a project for your App Engine app as follows:
- Visit the Google Cloud Platform Console and click Create Project.
- Supply the desired project name in the New Project form. It doesn't have to match your app name, but using the same name as your app might make administration easier.
- Accept the generated project ID or supply your own ID. This project ID is used as the App Engine application ID. Note that this ID can only be used once: if you subsequently delete your project, you won't be able to re-use the ID in a new project.
Note the application ID (project ID) you created above.
Upload your finished application to Google App Engine by invoking the following command. This opens a browser window for you to sign in using your Google account. You'll be providing the project ID as the argument for
appcfg.py -A <YOUR_PROJECT_ID_> update myapp/
Your app is now deployed and ready for users!
You have completed this tutorial.
The full URL for your application is
http://_your-app-id_.appspot.com/. Optionally, you can instead purchase and use a top-level domain name for your app, or use one that you have already registered.
Expanding on Hello, World!
In the "Creating a Guestbook" tutorial, you expand this simple application to become a fully-fledged Guestbook application that lets authenticated Google accounts post messages to a public page.