zhangyuchi / golang-templates-example

A simple Golang templates example

Home Page:https://rockfloat.com/post/learning-golang-templates.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A simple Golang templates example

Go Report Card Build Status

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 generate
go build && ./golang-templates-example
curl http://localhost:8080

About

A simple Golang templates example

https://rockfloat.com/post/learning-golang-templates.html


Languages

Language:Go 83.8%Language:HTML 16.2%