GAE/Golang quickstart with Bootstrap templates

Golang makes it easy to return a simplistic web page over a URL using the http/template package with a few lines of code.

It’s also easy to render content in nested templates with a few changes. However, the documentation is a little less obvious on this point, so it took some digging to get it working.

Quickstart webapp for App Engine

The demo running at https://bootstrap-dot-m0d3rnc0ad.appspot.com/, demonstrates the ability to render templated pages inheriting all the Bootstrap components, like so: Showcase pagel

Getting the quickstart code

The starter project lives in my public git repo and anyone can use it as starting point for their own GAE + Go + Bootstrap project.

Alternatively you can fetch the same quickstart code with go get. And if you already have the Go GAE SDK installed, you can immediately run it:

go get github.com/TheAndruu/Go-App-Engine-Bootstrap-Quickstart
cd $GOPATH/src/github.com/TheAndruu/Go-App-Engine-Bootstrap-Quickstart
goapp serve

Navigating a browser to localhost:8080 then displays the welcome page: Welcome pagel

Overview of how it works

The project uses the standard http/template library to render pages. Getting them to “nest” properly was something I had to research.

Moreover, App Engine needed to be configured to correctly serve the static content for Bootstraps Javascript, CSS, and fonts. app.yaml has the details for this.

The webapp has 2 pages: “/” (Welcome), and “/showcase” (Showcase).

Both pages inherit their layout from a base template theme.html to supply their respective content.

The application’s init() method initializes the templates such that any *.html file inside the templates directory will be picked up and available for reference automatically.

Templates are defined in the HTML files using the standard Go http/template syntax:

{{ define "outerTheme" }}
    <div>whatever you want in your theme</div>
{{ end }}

Templates are then nested / included using:

{{ template "content" . }}
// Don't forget the period (.) above

Extending the starter project

After checking out the code, it can be copied to a new location for your own project and extended by adding additional *.html files in the layouts directory.

From there, it’s all standard webapp development in Golang, such as how routes are defined in the defineRoutes() function.

Happy coding!

◀   Setting up a Go webapp on App Engine How to set up free SSL certificates with Let's Encrypt   ▶