Go标准库:深入剖析Go template(下)

本文涉及的产品
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: Go标准库:深入剖析Go template(下)

如果t1和t2的Parse()中,都定义一个或多个name相同的模板会如何?例如:


t1 := template.New("test1")
t2 := t1.New("test2")
t1, _= t1.Parse(
    `{{define "T1"}}ONE{{end}}
    {{define "T2"}}TWO{{end}}
    {{define "T3"}}{{template "T1"}} {{template "T2"}}{{end}}
    {{template "T3"}}`)
t2, _= t2.Parse(
    `{{define "T4"}}ONE{{end}}
    {{define "T2"}}TWOO{{end}}
    {{define "T3"}}{{template "T4"}} {{template "T2"}}{{end}}
    {{template "T3"}}`)
    _= t1.Execute(os.Stdout, "a")
    _= t2.Execute(os.Stdout, "a")

在上面的t1和t2中,它们共享同一个common,且t1.Parse()中定义了T1、T2和T3,t2.Parse()中定义了T4、T2和T3,且两个T2的解析内容不一样(解析树不一样)。

因为T1、T2、T3、T4都会加入到t1和t2共享的common中,所以无论是通过t1还是通过t2这两个关联名称都能找到T1、T2、T3、T4。但是后解析的会覆盖先解析的,也就是说,无论是t1.Lookup("T2")还是 t2.Lookup("T2")得到的T2对应的template,都是在t2.Parse()中定义的。当 t1.Execute()的时候,会得到t2中定义的T2的值。

1. ONE TWOO
2. ONE TWOO


Parse()

Parse(string)方法用于解析给定的文本内容string。用法上很简单,前面也已经用过几次了,没什么可解释的。重点在于它的作用。


**当创建了一个模板对象后,会有一个与之关联的common(如果不存在,template包中的各种函数、方法都会因为调用init()方法而保证common的存在)。**只有在Parse()之后,才会将相关的template name放进common中,表示这个模板已经可用了,或者称为已经定义了(defined),可用被Execute()或ExecuteTemplate(),也表示可用使用Lookup()和DefinedTemplates()来检索模板。另外,调用了Parse()解析后,会将给定的FuncMap中的函数添加到common的FuncMap中,只有添加到common的函数,才可以在模板中使用。


Parse()方法是解析字符串的,且只解析New()出来的模板对象。如果想要解析文件中的内容,见后文ParseFiles()、ParseGlob()。


Lookup()、DefinedTemplates()和Templates()方法


这三个方法都用于检索已经定义的模板,Lookup()根据template name来检索并返回对应的template,DefinedTemplates()则是返回所有已定义的templates。Templates()和DefinedTemplates()类似,但是它返回的是 []*Template,也就是已定义的template的slice。

前面多次说过,只有在解析之后,模板才加入到common结构中,才算是已经定义,才能被检索或执行。

当检索不存在的templates时,Lookup()将返回nil。当common中没有模板,DefinedTemplates()将返回空字符串"",Templates()将返回空的slice

func main() {
    t1 := template.New("test1")
    t2 := t1.New("test2")
    t1, _= t1.Parse(
        `{{define "T1"}}ONE{{end}}
        {{define "T2"}}TWO{{end}}
        {{define "T3"}}{{template "T1"}} {{template "T2"}}{{end}}
        {{template "T3"}}`)
    t2, _= t2.Parse(
        `{{define "T4"}}ONE{{end}}
        {{define "T2"}}TWOO{{end}}
        {{define "T3"}}{{template "T4"}} {{template "T2"}}{{end}}
        {{template "T3"}}`)
    fmt.Println(t1.DefinedTemplates())
    fmt.Println(t2.DefinedTemplates())
    fmt.Println(t2.Templates())
}

返回结果:


; defined templates are:"T1", "T2", "T3", "test1", "T4", "test2"
; defined templates are:"test1", "T4", "test2", "T1", "T2", "T3"
[0xc04201c2800xc0420641000xc04201c1c00xc04201c2c00xc04201c3000xc042064080]


从结果可见,返回的顺序虽然不一致,但包含的template name是完全一致的。

Clone()方法


Clone()方法用于克隆一个完全一样的模板,包括common结构也会完全克隆

t1 := template.New("test1")
t1 = t1.Parse(...)
t2 := t1.New("test2")
t2 = t2.Parse(...)
t3, err := t1.Clone()
if err != nil {
    panic(err)
}


这里的t3和t1在内容上完全一致,但在内存中它们是两个不同的对象。但无论如何,目前t3中会包含t1和t2共享的common,即使t2中定义了 {{define "Tx"}}...{{end}},这个Tx也会包含在t3中。

因为是不同的对象,所以修改t3,不会影响t1/t2。

看下面的例子

func main() {
    t1 := template.New("test1")
    t2 := t1.New("test2")
    t1, _= t1.Parse(
        `{{define "T1"}}ONE{{end}}
        {{define "T2"}}TWO{{end}}
        {{define "T3"}}{{template "T1"}} {{template "T2"}}{{end}}
        {{template "T3"}}`)
    t2, _= t2.Parse(
        `{{define "T4"}}ONE{{end}}
        {{define "T2"}}TWOO{{end}}
        {{define "T3"}}{{template "T4"}} {{template "T2"}}{{end}}
        {{template "T3"}}`)
    t3, err := t1.Clone()
    if err != nil {
        panic(err)
    }
    // 结果完全一致
    fmt.Println(t1.Lookup("T4"))
    fmt.Println(t3.Lookup("T4"))
    // 修改t3
    t3,_= t3.Parse(`{{define "T4"}}one{{end}}`)
    // 结果将不一致
    fmt.Println(t1.Lookup("T4"))
    fmt.Println(t3.Lookup("T4"))
}


Must()函数


正常情况下,很多函数、方法都返回两个值,一个是想要返回的值,一个是err信息。template包中的函数、方法也一样如此。

但有时候不想要err信息,而是直接取第一个返回值,并赋值给变量。操作大概是这样的:

123456
t1 := template.New("ttt")
t1,err := t1.Parse(...)
if err != nil {
    panic(err)
}
...

Must()函数将上面的过程封装了,使得Must()可以简化上面的操作:

1. func Must(t *Template, errerror) *Template {
2.     iferr != nil {
3.         panic(err)
4.     }
5.     return t
6. }


当某个返回 *Template,err的函数、方法需要直接使用时,可用将其包装在Must()中,它会自动在有err的时候panic,无错的时候只返回其中的 *Template


这在赋值给变量的时候非常简便,例如:

var t = template.Must(template.New("name").Parse("text"))



ParseFiles()和ParseGlob()


Parse()只能解析字符串,要解析文件中的内容,需要使用ParseFiles()或ParseGlob()。

template包中有ParseFiles()和ParseGlob()函数,也有ParseFiles()和ParseGlob()方法。


aef60bd807b5864b587c4d1bd7330949.png


这两个函数和这两个方法的区别,看一下文档就很清晰:

$ go doc template.ParseFiles
func ParseFiles(filenames ...string) (*Template, error)
    ParseFilescreatesanewTemplateandparsesthetemplatedefinitionsfrom
    thenamedfiles. Thereturnedtemplate'snamewillhavethe (base) nameand
    (parsed) contentsofthefirstfile. Theremustbeatleastonefile. Ifan
    erroroccurs, parsingstopsandthereturned *Templateisnil.
$godoctemplate.template.ParseFilesfunc (t *Template) ParseFiles(filenames ...string) (*Template, error)
    ParseFilesparsesthenamedfilesandassociatestheresultingtemplates
    witht. Ifanerroroccurs, parsingstopsandthereturnedtemplateisnil;
    otherwise it is t. There must be at least one file.


解释很清晰。ParseFiles()函数是直接解析一个或多个文件的内容,并返回第一个文件名的basename作为Template的名称,也就是说这些文件的template全都关联到第一个文件的basename上。ParseFiles()方法则是解析一个或多个文件的内容,并将这些内容关联到t上。


看示例就一目了然。

例如,当前go程序的目录下有3个文件:a.cnf、b.cnf和c.cnf,它们的内容无所谓,反正空内容也可以解析。

func main() {
    t1,err := template.ParseFiles("a.cnf","b.cnf","c.cnf")
    if err != nil {
        panic(err)
    }
    fmt.Println(t1.DefinedTemplates())
    fmt.Println()
    fmt.Println(t1)
    fmt.Println(t1.Lookup("a.cnf"))
    fmt.Println(t1.Lookup("b.cnf"))
    fmt.Println(t1.Lookup("c.cnf"))
}

输出结果:

; defined templates are: "a.cnf", "b.cnf", "c.cnf"&{a.cnf 0xc0420ae0000xc042064140  }
&{a.cnf 0xc0420ae0000xc042064140  }
&{b.cnf 0xc0420bc0000xc042064140  }
&{c.cnf 0xc0420bc1000xc042064140  }

从结果中可以看到,已定义的template name都是文件的basename,且t1和a.cnf这个template是完全一致的,即t1是文件列表中的第一个模板对象。

结构如下图:


e43c7a169ec6cc4d475917af3b1a0280.png

理解了ParseFiles()函数,理解ParseFiles()方法、ParseGlob()函数、ParseGlob()方法,应该不会再有什么问题。但是还是有需要注意的地方:

func main() {
    t1 := template.New("test")
    t1,err := t1.ParseFiles("a.cnf","b.cnf","c.cnf")
    if err != nil {
        panic(err)
    }
    // 先注释下面这行//t1.Parse("")
    fmt.Println(t1.DefinedTemplates())
    fmt.Println()
    fmt.Println(t1)
    fmt.Println(t1.Lookup("a.cnf"))
    fmt.Println(t1.Lookup("b.cnf"))
    fmt.Println(t1.Lookup("c.cnf"))
}


执行结果:

; defined templates are: "a.cnf", "b.cnf", "c.cnf"&{test <nil>0xc0420640c0  }
&{a.cnf 0xc0420b00000xc0420640c0  }
&{b.cnf 0xc0420be0000xc0420640c0  }
&{c.cnf 0xc0420be1000xc0420640c0  }

发现template.New()函数创建的模板对象test并没有包含到common中。为什么?

因为t.ParseFiles()、t.ParseGlob()方法的解析过程是独立于t之外的,它们只解析文件内容,不解析字符串。而New()出来的模板,需要Parse()方法来解析才会加入到common中。

将上面的注释行取消掉,执行结果将如下:

; defined templates are: "a.cnf", "b.cnf", "c.cnf", "test"&{test 0xc0420bc2000xc0420640c0  }
&{a.cnf 0xc0420ae0000xc0420640c0  }
&{b.cnf 0xc0420bc0000xc0420640c0  }
&{c.cnf 0xc0420bc1000xc0420640c0  }

具体原因可分析parseFiles()源码:

func parseFiles(t *Template, filenames ...string) (*Template, error) {
    if len(filenames) ==0 {
        // Not really a problem, but be consistent.return nil, fmt.Errorf("template: no files named in call to ParseFiles")
    }
    for_, filename := range filenames {
        b, err := ioutil.ReadFile(filename)
        if err != nil {
            return nil, err
        }
        s :=string(b)
        // name为文件名的basename部分
        name := filepath.Base(filename)
        var tmpl *Template
        if t == nil {
            t = New(name)
        }
        // 如果调用t.Parsefiles(),则t.Name不为空// name也就不等于t.Name// 于是新New(name)一个模板对象给tmplif name == t.Name() {
            tmpl = t
        } else {
            tmpl = t.New(name)
        }
        // 解析tmpl。如果选中了上面的else分支,则和t无关_, err = tmpl.Parse(s)
        if err != nil {
            return nil, err
        }
    }
    return t, nil
}

Execute()和ExecuteTemplate()


这两个方法都可以用来应用已经解析好的模板,应用表示对需要评估的数据进行操作,并和无需评估数据进行合并,然后输出到io.Writer中:

func (t *Template) Execute(wr io.Writer, data interface{}) errorfunc (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) error

两者的区别在于Execute()是应用整个common中已定义的模板对象,而ExecuteTemplate()可以选择common中某个已定义的模板进行应用。

例如:

func main() {
    t1 := template.New("test1")
    t1, _= t1.Parse(`{{define "T1"}}ONE{{end}}
        {{- define "T2"}}TWO{{end}}
        {{- define "T3"}}{{template "T1"}} {{template "T2"}}{{end}}
        {{- template "T3"}}`)
    _= t1.Execute(os.Stdout,"")
    fmt.Println()
    fmt.Println("-------------")
    _= t1.ExecuteTemplate(os.Stdout, "T2", "")
}

输出结果:

1. ONE TWO
2. -------------
3. TWO


FuncMap和Funcs()


template内置了一系列函数,但这些函数毕竟有限,可能无法满足特殊的需求。template允许我们定义自己的函数,添加到common中,然后就可以在待解析的内容中像使用内置函数一样使用自定义的函数。

自定义函数的优先级高于内置的函数优先级,即先检索自定义函数,再检索内置函数。也就是说,如果自定义函数的函数名和内置函数名相同,则内置函数将失效。

本文只对此稍作解释,本文的重点不是template的具体语法和用法。

在common结构中,有一个字段是FuncMap类型的:

type common struct {
    tmpl   map[string]*Template
    option option
    muFuncs    sync.RWMutex // protects parseFuncs and execFuncs
    parseFuncs FuncMap
    execFuncs  map[string]reflect.Value
}

这个类型的定义为:

type FuncMap map[string]interface{}


它是一个map结构,key为模板中可以使用的函数名,value为函数对象(为了方便称呼,这里直接成为函数)。函数必须只有1个值或2个值,如果有两个值,第二个值必须是error类型的,当执行函数时err不为空,则执行自动停止。

函数可以有多个参数。假如函数str有两个参数,在待解析的内容中调用函数str时,如果调用方式为 {{str . "aaa"}},表示第一个参数为当前对象,第二个参数为字符串"aaa"。

假如,要定义一个将字符串转换为大写的函数,可以:

import"strings"funcupper(str string)string {
    return strings.ToUpper(str)
}

然后将其添加到FuncMap结构中,并将此函数命名为"strupper",以后在待解析的内容中就可以调用"strupper"函数。

1. funcMap := template.FuncMap{
2.     "strupper": upper,
3. }


或者,直接将匿名函数放在FuncMap内部:

1. funcMap := template.FuncMap{
2.     "strupper": func(str string) string { return strings.ToUpper(str) },
3. }


现在只是定义了一个FuncMap实例,这个实例中有一个函数。还没有将它关联到模板,严格地说还没有将其放进common结构。要将其放进common结构,调用Funcs()方法(其实调用此方法也没有将其放进common,只有在解析的时候才会放进common):

func(t *Template)Funcs(funcMap FuncMap) *Template


例如:

funcMap := template.FuncMap{
    "strupper": func(str string) string { return strings.ToUpper(str) },
}
t1 := template.New("test")
t1 = t1.Funcs(funcMap)


这样,和t1共享common的所有模板都可以调用"strupper"函数。

注意,必须在解析之前调用Funcs()方法,在解析的时候会将函数放进common结构。

下面是完整的示例代码:


package main
import (
    "os""strings""text/template"
)
funcmain() {
    funcMap := template.FuncMap{
        "strupper": upper,
    }
    t1 := template.New("test1")
    tmpl, err := t1.Funcs(funcMap).Parse(`{{strupper .}}`)
    if err != nil {
        panic(err)
    }
    _ = tmpl.Execute(os.Stdout, "go programming")
}
funcupper(str string)string {
    return strings.ToUpper(str)
}


上面调用了 {{strupper .}},这里的strupper是我们自定义的函数,"."是它的参数(注意,参数不是放进括号里)。这里的"."代表当前作用域内的当前对象,对于这个示例来说,当前对象就是那段字符串对象"go programming"。

相关文章
|
16天前
|
Shell Go API
Go语言grequests库并发请求的实战案例
Go语言grequests库并发请求的实战案例
|
2月前
|
Rust 安全 算法
Go标准库的新 math/rand
Go标准库的新 math/rand
|
2月前
|
JSON 中间件 Go
go语言后端开发学习(四) —— 在go项目中使用Zap日志库
本文详细介绍了如何在Go项目中集成并配置Zap日志库。首先通过`go get -u go.uber.org/zap`命令安装Zap,接着展示了`Logger`与`Sugared Logger`两种日志记录器的基本用法。随后深入探讨了Zap的高级配置,包括如何将日志输出至文件、调整时间格式、记录调用者信息以及日志分割等。最后,文章演示了如何在gin框架中集成Zap,通过自定义中间件实现了日志记录和异常恢复功能。通过这些步骤,读者可以掌握Zap在实际项目中的应用与定制方法
go语言后端开发学习(四) —— 在go项目中使用Zap日志库
|
2月前
|
存储 JSON 前端开发
一文搞懂 Go 1.21 的日志标准库 - slog
一文搞懂 Go 1.21 的日志标准库 - slog
58 2
|
2月前
|
Prometheus Cloud Native Go
Go 1.22 标准库 slices 新增函数和一些旧函数增加新特性
Go 1.22 标准库 slices 新增函数和一些旧函数增加新特性
|
2月前
|
XML Go 数据库
|
2月前
|
Go
【go笔记】标准库-strconv
【go笔记】标准库-strconv
|
2月前
|
Go 索引
【go笔记】标准库-strings
【go笔记】标准库-strings
|
2月前
|
Go
【go笔记】使用标准库flag解析命令行参数
【go笔记】使用标准库flag解析命令行参数
|
3月前
|
Go
go使用snmp库查询mib数据
go使用snmp库查询mib数据
59 0

相关实验场景

更多
下一篇
无影云桌面