Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(template(main.go)): use go:embed #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
8 changes: 6 additions & 2 deletions template/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
package main

import (
"embed"
"html/template"
"log"
"net/http"
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down