From c3fcc590ed0bb09d5e89813569f7c39e86af56fd Mon Sep 17 00:00:00 2001 From: "daniel.orbach" Date: Sun, 25 Apr 2021 14:33:54 +0300 Subject: [PATCH 1/2] go.mod: set -lang to go1.16 Upgrade to golang-1.16 --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index c6b3f6b0..9e0bad31 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module golang.org/x/example -go 1.15 +go 1.16 require golang.org/x/tools v0.0.0-20210112183307-1e6ecd4bf1b0 From ff7b227ef2bf33f6ef6272ff36b317090b93547f Mon Sep 17 00:00:00 2001 From: "daniel.orbach" Date: Sun, 25 Apr 2021 14:35:03 +0300 Subject: [PATCH 2/2] template: embed .tmpl Embed template files using `go:embed`. --- template/main.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/template/main.go b/template/main.go index 5da54752..f1cc167e 100644 --- a/template/main.go +++ b/template/main.go @@ -23,6 +23,7 @@ limitations under the License. package main import ( + "embed" "html/template" "log" "net/http" @@ -35,10 +36,13 @@ func main() { log.Fatal(http.ListenAndServe("localhost:8080", nil)) } +//go:embed index.tmpl image.tmpl +var templateFS embed.FS + // indexTemplate is the main site template. // The default template includes two template blocks ("sidebar" and "content") // that may be replaced in templates derived from this one. -var indexTemplate = template.Must(template.ParseFiles("index.tmpl")) +var indexTemplate = template.Must(template.ParseFS(templateFS, "index.tmpl")) // Index is a data structure used to populate an indexTemplate. type Index struct { @@ -70,7 +74,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { // imageTemplate is a clone of indexTemplate that provides // alternate "sidebar" and "content" templates. -var imageTemplate = template.Must(template.Must(indexTemplate.Clone()).ParseFiles("image.tmpl")) +var imageTemplate = template.Must(template.Must(indexTemplate.Clone()).ParseFS(templateFS, "image.tmpl")) // Image is a data structure used to populate an imageTemplate. type Image struct {