内容简介:在gin融合sqlx包时,发现在写复杂的sql的时候会比较复杂,而在java一直比较钟爱mybatis编写sql,希望在golang也能找到类似的做法,在查阅很多博客看到使用模板进行类似mybatis xml生成sql,所以先把原先学习语法没有进行学习的模板进行一些储备,只是一部分认为能用到的一些知识导入模板文件1.导入单个文件
背景
在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
Age uint
}
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}}
以上所述就是小编给大家介绍的《golang template 用作类似mybatis前期一些准备》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 《代码大全》读书笔记-构建的前期
- 创业公司基础设施如何搭建(一):前期准备
- [译] 不要把 JWT 用作 session
- 使用typescript开发react-native前期踩坑记录
- 如何在Kubernetes中将Envoy用作负载均衡器
- K近邻算法用作回归的使用介绍(使用Python代码)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。