Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory structure for Go web app

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?

like image 804
Wesley Avatar asked Nov 19 '14 05:11

Wesley


People also ask

Where do you put go projects?

If GOPATH is set, binaries are installed to the bin subdirectory of the first directory in the GOPATH list. Otherwise, binaries are installed to the bin subdirectory of the default GOPATH ( $HOME/go or %USERPROFILE%\go ).


1 Answers

Update 2021, with Go 1.16, you would use:

The new embed package provides access to files embedded in the program during compilation using the new //go:embed directive.

See "Embedding files in Go using the "embed" package", from Amit Saha (SRE at @Atlassian) who does embed a template in his project.

//go:embed templates/main.go.tmpl
var tmplMainGo []byte

This makes the contents of the above template available as a slice of bytes in the tmplMainGo variable.

We can then access the template as follows:

tmpl, err := tmpl.Parse(string(tmplMainGo))

See a demo at amitsaha/go-embed.

See also "Working with Embed in Go 1.16 Version" from Barak Amar and his own demo project.


Original answer2014: You could consider using the project jteeuwen/go-bindata (archived in 2018)

This package converts any file into managable Go source code. Useful for embedding binary data into a go program. The file data is optionally gzip compressed before being converted to a raw byte slice.

You can see it used in "golang embed file for later parsing execution use"

That same page also mention GeertJohan/go.rice as an alternative

Another recent good tool comes from esc: Embedding Static Assets in Go

a program that:

  • can take some directories and recursively embed all files in them in a way that was compatible with http.FileSystem
  • can optionally be disabled for use with the local file system for local development
  • will not change the output file on subsequent runs
  • has reasonable-sized diffs when files changed
  • is vendoring-friendly
like image 74
VonC Avatar answered Sep 29 '22 22:09

VonC