html/template 和 text/template区别

前言

最近在学习go template,跟着一篇文章进行例程的学习,结果发现无论怎么调试,都没有办法复现例程的打印结果

纠结了一天后,求助同事发现了其中的端倪

text/template 是将内容都已text文本格式返回。

html/tempalte针对的是需要返回HTML内容的场景。
在模板渲染过程中会对一些有风险的内容进行转义,以此来防范跨站脚本攻击。

html/template

例程中给出的代码如下

package main

import (
	"fmt"
	// "text/template"
	"html/template"
)

func main() {

	// ******ParseFiles()方法*****
	t1 := template.New("test")
	t1, err := t1.ParseFiles("a.cnf", "b.cnf", "c.cnf")

	// ******ParseFiles()定义*****
	// t1, err := template.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"))
}

理论打印结果应该是:
在这里插入图片描述

但是可以看到我们跑程序后打印结果如下:
在这里插入图片描述
出现了很大的不一致,这时,我百思不得起解,无论怎么样单步调试,修改输出,都没有办法得到例程输出

最后在同事的提醒下,查看函数的源码,直接F12

首先是Template.New()

// New allocates a new HTML template with the given name.
func New(name string) *Template {
	ns := &nameSpace{set: make(map[string]*Template)}
	ns.esc = makeEscaper(ns)
	tmpl := &Template{
		nil,
		template.New(name),
		nil,
		ns,
	}
	tmpl.set[name] = tmpl
	return tmpl
}

New返回tmpl,tmpl结构体中我们可以改变的是name,也就是存储我们输入的“test”

这里暂时看不出来端倪,继续看后面的t1.DefinedTemplates(),可以发现也正常,这块输出确实也是对的

func (t *Template) DefinedTemplates() string {
	return t.text.DefinedTemplates()
}

那么问题出在哪里呢?

随着调试过程逐渐深入,发现端倪或许在Template
在这里插入图片描述
进入到Template,可以看如下结构体

// Template is a specialized Template from "text/template" that produces a safe
// HTML document fragment.
type Template struct {
	// Sticky error if escaping fails, or escapeOK if succeeded.
	escapeErr error
	// We could embed the text/template field, but it's safer not to because
	// we need to keep our version of the name space and the underlying
	// template's in sync.
	text *template.Template
	// The underlying template's parse tree, updated to be HTML-safe.
	Tree       *parse.Tree
	*nameSpace // common to all associated templates
}

无论如何,我们的fmt.Println(t1)打印代码也是按照顺序打印Template结构体,结构体中第一个是erro,第二个是template.Template,第三个是parse.Tree,第四个是*nameSpace

这样也就能解释为什么输出结果是

&{<nil> 0xc00007e0c0 0xc0001085a0 0xc00005c180}

话说回来,也就是我们的这个结构体压根就没有输出text值,只有erro和内存地址值

跟text有关的我们可以看到在text/template中

在这里插入图片描述

text/template

源码看到这里就豁然开朗了

结构体中第一个就是name的字符串

// Template is the representation of a parsed template. The *parse.Tree
// field is exported only for use by html/template and should be treated
// as unexported by all other clients.
type Template struct {
	name string
	*parse.Tree
	*common
	leftDelim  string
	rightDelim string
}

所以,我们只需要修改我们的引用包位置就可以解决和例程输出不一致的问题

package main

import (
	"fmt"
	"text/template"
	// "html/template"
)

func main() {

	// ******ParseFiles()方法*****
	t1 := template.New("test")
	t1, err := t1.ParseFiles("a.cnf", "b.cnf", "c.cnf")

	// ******ParseFiles()定义*****
	// t1, err := template.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"))
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值