Skip to content

Instantly share code, notes, and snippets.

@amattn
Created September 3, 2011 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amattn/1190308 to your computer and use it in GitHub Desktop.
Save amattn/1190308 to your computer and use it in GitHub Desktop.
Golang New (2011) template example go & template File
type Entry struct {
Title string
Content string
}
entry := make(Entry)
entry.Title = "New Go Templates (mid-2011)"
entry.Content = "A trivial example of how to use them. see documentation and Go source code/tests for more examples"
template, err := template.ParseFile(templatePath)
if err != nil {
fmt.Println("ERROR template.ParseFile: %v", err)
return
}
buffer := new(bytes.Buffer)
err = template.Execute(buffer, entry)
if err != nil {
fmt.Println("ERROR template.Execute: %v", err)
return
}
------------------------------------------------------
<html>
<head>
<title>{{.Title}}</title>
<body>
<h1>{{.Title}}</h1>
{{.Content}}
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment