Created
September 3, 2011 00:51
-
-
Save amattn/1190308 to your computer and use it in GitHub Desktop.
Golang New (2011) template example go & template File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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