I've followed the Writing Web Applications tutorial on the Go website and I'm starting to write my own web app. I've also read the beginning of How to Write Go Code and am trying to organise my code with the same workspace structure.
I'm writing a simple web app named mygosite that handles all requests by rendering a single template. After running go install github.com/wesleym/mygosite
, my directory structure now looks like this:
go
+-src
| +-github.com
| +-wesleym
| +-mygosite
| +-mygosite.go
| +-templates
| +- index.html
| +-.git
+-bin
+-mygosite
In my code, I'm referring to the template with path templates/index.html
. When I run bin/mygosite
, the app can't find my template because it's in the source tree. Since the templates are important to my app, I don't want to move them outside of my mygosite git repository.
Is my directory layout reasonable given what I'm trying to do? Should I change the path to my template in my code to be src/github.com/wesleym/mygosite/templates/index.html
? What about static assets like CSS and JavaScript - where should those go when the time comes to introduce them?
tl;dr: Where do I put templates and static files in a Go web application project?
Where do I put templates and static files in a Go web application project?
is the "wrong" question. If you ask "How will my executable find its resources?" you are almost at the solution: Your executable should read its resources from a (or several) locations which are configurable (and it is always nice to provide sensible defaults). Command line flags, environment variables and config files in the current working directory are common for such tasks. (Of course, if you just have a handful of small resources: Pack them into your executable as VonC recommended; this scheme just breaks down once you start including large assets like images or video.)