go go Go Template模板使用 Alex 2017-07-29 2023-10-28 go template模板使用 字符变量替换解析 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package main import ( 	"os" 	"text/template" ) func main() { 	name := "qipajun" 	tmpl, err := template.New("test").Parse("hello, {{.}}") //建立一个test模板,内容是"hello, 使用{{.}}替换变量内容" 	if err != nil { 		panic(err) 	} 	err = tmpl.Execute(os.Stdout, name)  //将string与模板合成,变量name的内容会替换掉{{.}} 	//合成结果放到os.Stdout里 	if err != nil { 		panic(err) 	} } //输出 :   hello, qipajun 
 
结构体类型模板解析替换 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package main import ( 	"os" 	"text/template" ) type article struct { 	Id	int	//首字母大写 对外开放访问 不然模板解析不到 	Title 	string } func main()  { 	art := article{10,"hello world"} 	//建立一个test模板,使用`{{.Id}}`替换结构体 article.Id 值 	tmpl, err := template.New("test").Parse("id: {{.Id}}, title: {{.Title}}") 	if err != nil { 		panic(err) 	} 	err = tmpl.Execute(os.Stdout, art) 	//合成结果放到os.Stdout里 	if err != nil { 		panic(err) 	} 	//输出 id: 10, title: hello world } 
 
遍历循环输出 1 2 3 4 5 {{range .}} {{end}} {{range $i, $v := .slice}} {{end}} 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package main import ( 	"os" 	"text/template" ) type article struct { 	Id	int	//首字母大写 对外开放访问 负责模板解析不到 	Title 	string } func main()  { 	art := article{10,"hello world"} 	var map4 [5]article 	map4[0] = art 	map4[1] = art 	const tpl = `<div><ul>{{range .}}<li><a href="./article-{{.Id}}.html" >{{.Title}}</a></li>{{end}}</ul></div>` 	tmpl, err := template.New("test").Parse(tpl) //建立一个模板,内容是"hello, {{.}}" 	if err != nil { 		panic(err) 	} 	err = tmpl.Execute(os.Stdout, map4) 	//合成结果放到os.Stdout里 	if err != nil { 		panic(err) 	} } 
 
多个模板互相应用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 package main import ( 	"os" 	"text/template" ) func main() { 	muban1 := `hi, {{template "M2"}}, hi, {{template "M3"}} ` 	muban2 := `我是模板2,{{template "M3"}}` 	muban3 := "ha我是模板3ha!" 	//建立一个`M1`模板,替换解析`muban1`字符 并且使用` {{template "M2"}}` 替换模板2内容 	tmpl, err := template.New("M1").Parse(muban1) 	if err != nil { 		panic(err) 	} 	//M2模板中 又使用` {{template "M3"}}` 替换模板3内容 	tmpl.New("M2").Parse(muban2) 	if err != nil { 		panic(err) 	} 	tmpl.New("M3").Parse(muban3) 	if err != nil { 		panic(err) 	} 	err = tmpl.Execute(os.Stdout, nil) 	if err != nil { 		panic(err) 	} 	//输出内容 	/** 	hi, 我是模板2,ha我是模板3ha!, 	hi, ha我是模板3ha! 	 */ } 
 
Parse 解析字符串模板  template.ParseFiles 从文件读取模板解析
 
if 1 2 3 4 5 {{if .Attended}}     true line {{else}}     false line {{end}} 
 
相关判断符号1 2 3 4 5 6 7 8 9 10 11 12    eq 	Returns the boolean truth of arg1 == arg2 ne 	Returns the boolean truth of arg1 != arg2 lt 	Returns the boolean truth of arg1 < arg2 le 	Returns the boolean truth of arg1 <= arg2 gt 	Returns the boolean truth of arg1 > arg2 ge 	Returns the boolean truth of arg1 >= arg2 
 
with 如果不为空就替换模板打印 如果空就不输出1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 package main import ( 	"text/template" 	"os" 	"fmt" ) func main()  { 	const letter = ` {{with .Gift}} 	 <p>{{.}}</p> {{end}} ` 	type Recipient struct { 		Name, Gift string 		Attended   bool 	} 	var recipients = []Recipient{ 		{"Aunt Mildred", "bone china tea set", true}, 		{"Uncle John", "moleskin pants", false}, 		{"Cousin Rodney", "", false}, 	} 	t := template.Must(template.New("letter").Parse(letter)) 	t.Execute(os.Stdout, recipients) 	// Execute the template for each recipient. 	for _, r := range recipients { 		err := t.Execute(os.Stdout, r) 		if err != nil { 			fmt.Println("executing template:", err) 		} 	} }