golang template 用作類似mybatis前期一些準備
背景
在gin融合sqlx包時,發現在寫複雜的sql的時候會比較複雜,而在java一直比較鍾愛mybatis編寫sql,希望在golang也能找到類似的做法,在查閱很多部落格看到使用模板進行類似mybatis xml生成sql,所以先把原先學習語法沒有進行學習的模板進行一些儲備,只是一部分認為能用到的一些知識
使用
匯入模板檔案
1.匯入單個檔案
template.ParseFiles("template_test.tpl")
2.匯入多個檔案(不同dao使用不同檔案表示)
template.ParseGlob("*.tpl")
3.加入自定義函式FuncMap
自定義函式必須在檔案匯入之前加入,否則會出錯,因為這個糾結了很久,都是因為不熟悉api
t = template.New("test") t.Funcs(template.FuncMap{"add": Add}) // 一定要在載入檔案之前 t.ParseGlob("*.tpl")
4.編寫簡單引入
在template_test.tpl寫入
{{define "getName"}} select * from name where name={{.Name}} and age={{.Age}} {{end}}
define用作子模組,可以用於dao裡面的各個方法命名,同一個檔案表達一個dao全部方法
golang進行引入執行
func GetName() { fmt.Println(t.Name()) user := &User{Name: "test", Age: 12} t.ExecuteTemplate(os.Stdout, "getName", user) }
輸出select * from name where name=test and age=12
5.編寫迴圈與自定義add方法使用
在template_test.tpl寫入
{{define "getNames"}} select * from name where 1=1 {{with .Name}}and name={{.}}{{end}} {{with .Ids}}{{range .}} and id={{.}}{{end}} {{end}} {{add 1 2}} {{end}}
注意上面使用了with 後續表示檢驗是否為空的值直接使用{{.}}
add方法在上面新增自定義函式時候已經引入,下面把定義的函式,與處理其他程式碼寫下
func Add(a int, b int) int { return a + b } type IDData struct { Name interface{} Ids[]int } func GetNames() { fmt.Println(t.Name()) data := &IDData{Name: "test", Ids: nil} err := t.ExecuteTemplate(os.Stdout, "getNames", data) if err != nil { fmt.Println(err) } }
6.其他內建的一些函式
在template_test.tpl寫入
{{define "funcAction"}} {{if eq .a 0}}a=0{{else}}a=1{{end}} {{if eq .b 0}}b=0{{else if eq .b 1}}b=1{{else}}b={{.b}}{{end}} {{if or (eq .a 0) (eq .b 2)}}true{{else}}false{{end}} {{if and (eq .a 0) (eq .b 2)}}true{{else}}false{{end}} {{end}}
上面主要有使用了if eq or and
golang程式碼
func FuncAction() { data := make(map[string]interface{}) data["a"] = 1 data["b"] = 2 if err := t.ExecuteTemplate(os.Stdout, "funcAction", data); err != nil { fmt.Println("error", err) } }
最後把測試程式碼貼下 自己也用作參考
go_template.go
package template import ( "bytes" "fmt" "html/template" "os" ) var t *template.Template type User struct { Name string Ageuint } func init() { //t, _ = template.ParseFiles("template_test.tpl") t = template.New("test") t.Funcs(template.FuncMap{"add": Add}) // 一定要在載入檔案之前 t.ParseGlob("*.tpl") } func Add(a int, b int) int { return a + b } func GetName() { fmt.Println(t.Name()) user := &User{Name: "test", Age: 12} t.ExecuteTemplate(os.Stdout, "getName", user) } type IDData struct { Name interface{} Ids[]int } func GetNames() { fmt.Println(t.Name()) data := &IDData{Name: "test", Ids: nil} err := t.ExecuteTemplate(os.Stdout, "getNames", data) if err != nil { fmt.Println(err) } } func InsertName() { fmt.Println(t.Name()) user := &User{Name: "test", Age: 14} buf := new(bytes.Buffer) t.ExecuteTemplate(buf, "insertName", user) sql := buf.String() fmt.Println("InsertName_sql:", sql) } func GetNameTwo() { fmt.Println(t.Name()) user := &User{Name: "test", Age: 12} t.ExecuteTemplate(os.Stdout, "getName1", user) } func InsertNameTwo() { fmt.Println(t.Templates()[0].Name()) user := &User{Name: "test", Age: 14} buf := new(bytes.Buffer) t.ExecuteTemplate(buf, "insertName1", user) sql := buf.String() fmt.Println("InsertName_sql:", sql) } func FuncAction() { data := make(map[string]interface{}) data["a"] = 1 data["b"] = 2 if err := t.ExecuteTemplate(os.Stdout, "funcAction", data); err != nil { fmt.Println("error", err) } }
go_template_test.go
package template import "testing" func TestGetName(t *testing.T) { GetName() } func TestInsertName(t *testing.T) { InsertName() } func TestGetNameTwo(t *testing.T) { GetNameTwo() } func TestInsertNameTwo(t *testing.T) { InsertNameTwo() } func TestGetNames(t *testing.T) { GetNames() } func TestFuncAction(t *testing.T) { FuncAction() }
template_test.tpl
{{define "getName"}} select * from name where name={{.Name}} and age={{.Age}} {{end}} {{define "insertName"}} insert into name (name,age) values ({{.Name}},{{.Age}}) {{end}} {{define "getNames"}} select * from name where 1=1 {{with .Name}}and name={{.}}{{end}} {{with .Ids}}{{range .}} and id={{.}}{{end}} {{end}} {{add 1 2}} {{end}} {{define "funcAction"}} {{if eq .a 0}}a=0{{else}}a=1{{end}} {{if eq .b 0}}b=0{{else if eq .b 1}}b=1{{else}}b={{.b}}{{end}} {{if or (eq .a 0) (eq .b 2)}}true{{else}}false{{end}} {{if and (eq .a 0) (eq .b 2)}}true{{else}}false{{end}} {{end}}
template_test1.tpl
{{define "getName1"}} select * from name1 where name={{.Name}} and age={{.Age}} {{end}} {{define "insertName1"}} insert into name1 (name,age) values ({{.Name}},{{.Age}}) {{end}}