package main;
import (
"html/template"
"os"
"fmt"
)
type Person struct {
Id string;
UserName string;
Age int;
Contact map[string]string;
}
func (p Person) Say(msg string) string {
return msg;
}
func test1() string {
return "test1";
}
func test2(msg string) string {
return msg + "test2";
}
func test3(a int) bool {
if a == 3 {
return true;
}
return false;
}
func sum() func(nums ...int) (int, error) {
return func(nums ...int) (int, error) {
sum := 0;
for _, v := range nums {
sum += v;
}
return sum, nil;
};
}
func main() {
//創建一個模板
t := template.New("test");
//注釋
t, _ = t.Parse(`{{/*我是注釋*/}}`);
t.Execute(os.Stdout, nil);
//輸出單個字符串
t2 := template.New("test");
// {{.}}輸出當前對象的值
t2, _ = t2.Parse(`{{.}}`);
p := "test";
//輸出字符串
t2.Execute(os.Stdout, p);
fmt.Println();
//輸出結構中字段的值
t3 := template.New("test");
// {{.字段名}}輸出對象中字段的值
//注意字段是可導出的,首字母大寫
t3, _ = t3.Parse(`{{.Id}} {{.UserName}} {{.Age}}`);
t3.Execute(os.Stdout, Person{"001", "test", 11, nil});
fmt.Println();
//調用結構的方法
t4 := template.New("test");
// {{.方法 參數1 參數2}}
//參數依次傳入方法,輸出返回值
t4, _ = t4.Parse(`{{.Say "hello"}}`);
t4.Execute(os.Stdout, Person{"002", "test2", 22, nil});
fmt.Println();
//模板中定義變量
t5 := template.New("test");
// {{$變量名}} 輸出模板中定義的變量
t5, _ = t5.Parse(`{{$a := "模板中定義的變量"}} {{$a}}`);
t5.Execute(os.Stdout, nil);
fmt.Println();
//模板函數
t6 := template.New("test");
//注冊模板函數
t6.Funcs(template.FuncMap{"test1": test1});
t6.Funcs(template.FuncMap{"test2": test2});
// {{函數名}}輸出函數返回值
// {{函數名 參數1 參數2}}
// {{.字段名|函數名}} 以字段的值作為函數的參數
t6, _ = t6.Parse(`
{{test1}}
{{test2 "參數"}}
{{.UserName|test2}}
`);
t6.Execute(os.Stdout, Person{"003", "test3", 33, nil});
fmt.Println();
//條件判斷
t7 := template.New("test");
t7.Funcs(template.FuncMap{"test3": test3});
// {{if 表達式}}{{else if}}{{else}}{{end}}
// if后面可以是一個條件表達式,可以是字符串或布爾值變量
// 注意if后面不能直接使用==來判斷
t7, _ = t7.Parse(`
{{if 1}}
為真
{{else}}
為假
{{end}}
{{$a := 4}}
{{if $a|test3}}
$a=3
{{else}}
$a!=3
{{end}}
`);
t7.Execute(os.Stdout, nil);
fmt.Println();
//遍歷
t8 := template.New("test");
// {{range 鍵,值 := 變量}}{{end}} 遍歷對象
// {{with 變量}}{{end}} 指定當前操作的對象
t8, _ = t8.Parse(`
{{range $k, $v := .Contact}}
{{$k}} {{$v}}
{{end}}
{{with .Contact}}
{{range $k, $v := .}}
{{$k}} {{$v}}
{{end}}
{{end}}
`);
con := make(map[string]string);
con["qq"] = "123456";
con["tel"] = "13888888888";
t8.Execute(os.Stdout, Person{Contact: con});
fmt.Println();
//嵌套模板
t9 := template.New("test");
t9.Funcs(template.FuncMap{"test1": test1});
// {{define "模板名"}}模板內容{{end}} 定義模板
// {{template "模板名"}} 引入模板
// {{template "模板名" 函數}} 將函數中的值賦給模板中的{{.}}
t9, _ = t9.Parse(`
{{define "tp1"}} 我是模板1 {{end}}
{{define "tp2"}} 我是模板2 {{.}} {{end}}
{{define "tp3"}} {{template "tp1"}} {{template "tp2"}} {{end}}
{{template "tp1"}}
{{template "tp2" test1}}
{{template "tp3" test1}}
`);
t9.Execute(os.Stdout, nil);
fmt.Println();
//內置的模板函數
t10 := template.New("test");
t10.Funcs(template.FuncMap{"sum": sum});
t10, _ = t10.Parse(`
/*如果3為真,返回4,否則返回3*/
{{and 3 4}}
/*call后第一個參數的返回值必須是一個函數*/
{{call sum 1 3 5 7}}
/*轉義文本中的html標簽*/
{{"<br>"|html}}
/*返回Contact索引為qq的值*/
{{index .Contact "qq"}}
/*返回用js的escape處理后的文本*/
{{"?a=123&b=你好"|js}}
/*返回參數的長度值*/
{{"hello"|len}}
/*返回單一參數的布爾否定值*/
{{not 0}}
/*如果3為真,返回3,否則返回4*/
{{or 3 4}}
/*fmt.Sprint的別名*/
{{"你好"|print "世界"}}
/*fmt.Sprintf的別名*
{{"你好"|printf "%d %s" 123}}
/*fmt.Sprintln的別名*/
{{"你好"|println "世界"}}
/*url中get參數轉義*/
{{"?q=關鍵字&p=1"|urlquery}}
/*等於*/
{{if eq 1 1}}
1=1
{{end}}
/*不等於*/
{{if ne 1 1}}
1!=1
{{end}}
/*小於*/
{{if lt 3 1}}
3<1
{{end}}
/*小於等於*/
{{if le 3 3}}
3<=3
{{end}}
/*大於*/
{{if gt 3 1}}
3>1
{{end}}
/*大於等於*/
{{if ge 3 3}}
3>=3
{{end}}
`);
con2 := make(map[string]string);
con2["qq"] = "123456";
con2["tel"] = "13888888888";
t10.Execute(os.Stdout, Person{Contact: con2});
}