Skip to content

jmcfarlane/golang-templates-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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