A simple Golang templates example
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
static
templates
.gitignore
.travis.yml
Dockerfile
Makefile
README.md
main.go
main_test.go

README.md

A simple Golang templates example

Go Report Card Build Status codecov

During the process of learning Golang templates, certain aspects were confusing to me. The goal of this little repo is to document what I eventually wound up doing. Hopefully with feedback this repo could serve as an example of at least one way to use templates effectively.

By no means is this intended to be a proper (or even correct) howto on Golang templates, rather it's just what I've learned so far. Here's what I was trying to accomplish:

  1. Have a directory of templates (header.html, foobar.html, etc).
  2. Have a directory of static files (css, images, etc).
  3. Use some templates as full pages (about.html, hello.html).
  4. Use some templates as partials (header.html, footer.html).
  5. Serve static content in a manner similar to http.FileServer.
  6. Exclude templates from the static files being served.
  7. Support custom template functions.
  8. Compile everything into a single static binary (including templates and static files).

Installation

go get github.com/GeertJohan/go.rice/rice
go get -d github.com/jmcfarlane/golang-templates-example

Run

cd $GOPATH/src/github.com/jmcfarlane/golang-templates-example
go get -t ./...
go generate
go test -v
go build
./golang-templates-example
curl http://localhost:8080

Optionally use make

If you have make installed, you can try this repo by doing:

make
./golang-templates-example