Last active
August 29, 2015 14:23
-
-
Save suntong/3539307ef77ddee95f10 to your computer and use it in GitHub Desktop.
Go Template Example
This file contains hidden or 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
package main | |
import ( | |
"bytes" | |
"os" | |
"text/template" | |
) | |
type Person struct { | |
Name string //exported field since it begins with a capital letter | |
} | |
func main() { | |
t := template.New("hello template") //create a new template with some name | |
t, _ = t.Parse("hello {{.Name}}!\n") //parse some content and generate a template, which is an internal representation | |
p := Person{Name: "Mary"} //define an instance with required field | |
t.Execute(os.Stdout, p) //merge template ‘t’ with content of ‘p’ | |
type dict struct { | |
// struct fields must be public | |
Title string | |
Release int | |
} | |
params := dict{Title: "Go", Release: 60} | |
t, _ = template.New("template_name").Parse( | |
"<h1>{{.Title}}</h1>r{{.Release}}\n") | |
buf := new(bytes.Buffer) | |
t.Execute(buf, params) | |
print(buf.String()) | |
t = template.New("template test") | |
t = template.Must(t.Parse("This is just static text. \n{{\"This is pipeline data - because it is evaluated within the double braces.\"}} {{`So is this, but within reverse quotes.`}}\n")) | |
t.Execute(os.Stdout, nil) | |
} | |
/* | |
hello Mary! | |
This is just static text. | |
This is pipeline data - because it is evaluated within the double braces. So is this, but within reverse quotes. | |
<h1>Go</h1>r60 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment