Starting to build a web app, I found precious little suggesting best practice with Go (or "golang" for search engines). This page therefore reflect what I am trying at the moment, somewhat influenced by my experiences with Rails, as well as this web page:
http://stackoverflow.com/questions/9573644/go-appengine-how-to-structure-templates-for-application
/
|
+---- app.yaml
+- static/
| |
| +- images/
| | |
| | +---- logo.png
| |
| +- stylesheets/
| |
| +---- styles.css
|
+- templates/
| |
| |
| +---- base.html
|
+- app/
| |
| |
| +---- http.go
| +- templates/
| | |
| | +---- index.html
| |
+- post/
| |
| |
| +---- http.go
| +---- model.go
| +- templates/
| | |
| | +---- index.html
| | +---- compose.html
| | +---- edit.html
| | +---- show.html
| |
etc.
The first templates folder holders the template with the header and any HTML common access the site. Each page will have its HTML inserted into this (like layouts in Rails).
The app folder is for the root page, /, so (for me) has no model, just an index page.
The post folder is an example of a folder for a model called post. In Ruby I would have a template called new, but new is a keyword in Go, so here I used compose instead (because I want the template name to correspond to the function).
No configuration
There is no global config folder or file besides app.yaml. Each model registers itself to accept specific HTTP requests, so an equivalent to routes.rb is not required. The datastore makes database configuration and migrations unnecessary. Go automatically runs any init() function in a package on start-up, so initialisation is better done there.
The up-shot is that you still have to do some configuration, but it is done locally in the model folder, which does seem a better way to organise it. Each model can be an isolated unit that you could drop into another application just by copying the folder across.
The app.yaml file
This is used by Google App Engine to configure the web app. It states the language used, and also has some URL pattern-matching that determines how web requests are handled. For a Python application, this can be used to direct requests to specific scripts, but it seems that for Go, you send them all to a script called _go_app. You can use it to distinguish between dynamic pages and static pages, and to restrict access to
either users logged in only and administators only, but once set up, you can probably just forget about it (except perhaps updating the version number).
https://developers.google.com/appengine/docs/go/config/appconfig
Static files
As the app.yaml file uses the term "static", I think it makes sense to keep all these in a single directory with that name, and to have subdirectories for files within that (images, stylesheets, etc.). To access an image, the request will be /images/logo.png, not /static/images/logo.png, by the way.