河南做外贸网站的公司简介,短视频网站建设方案,一个vps主机放两个网站 速度,长治长治那有做网站的我们都知道在golang的模板语法中#xff0c;我们可以使用template关键字嵌套其他模块#xff0c; 如#xff1a; {{template 模板文件名 .}} 然而#xff0c;这里的 “模板文件名” 是不能使用变量的#xff01; 注意这里最后的的 . 这个实际上是templa…我们都知道在golang的模板语法中我们可以使用template关键字嵌套其他模块 如 {{template 模板文件名 .}} 然而这里的 “模板文件名” 是不能使用变量的 注意这里最后的的 . 这个实际上是template关键字的第二个参数 在go的模板里面 . 它代表的是当前的所有变量数据即你在执行渲染模板时的第二个data参数函数原型 func (t *template.Template) Execute(wr io.Writer, data any) error 了解这点很重要
底层源码分析
golang的template模板解析底层是通过一个叫做 lexer 的对象来实现的。在lexer里面这里的 template 被作为一个关键字 和其他的 if else end, range ,with等一样的关键字。 而这里的模板名在go底层在解析前就被使用Quote解码了 即 strconv.Quote(模板文件名)所以你在这里输入的任何变量都不会被解析
go底层代码见源码文件 /src/text/template/parse/node.go
template这个关键字的第二个参数的处理代码
func (t *TemplateNode) writeTo(sb *strings.Builder) {sb.WriteString({{template )sb.WriteString(strconv.Quote(t.Name))if t.Pipe ! nil {sb.WriteByte( )t.Pipe.writeTo(sb)}sb.WriteString(}})
} lexer模板渲染里面的关键字参考
可以看到这里有我们熟悉的 . if else range with 还有template 这些在golang的模板里面都被用作了关键字
var key map[string]itemType{.: itemDot,block: itemBlock,break: itemBreak,continue: itemContinue,define: itemDefine,else: itemElse,end: itemEnd,if: itemIf,range: itemRange,nil: itemNil,template: itemTemplate,with: itemWith,
} lexer模板分隔符解析逻辑和默认分隔符参考
可见对于模板分隔符如果我们传递的是一个空字符串则lexer就会使用他自己 定义的常量 {{ }}来作为分隔符
// state functionsconst (leftDelim {{rightDelim }}leftComment /*rightComment */
)// lex creates a new scanner for the input string.
func lex(name, input, left, right string) *lexer {if left {left leftDelim}if right {right rightDelim}l : lexer{name: name,input: input,leftDelim: left,rightDelim: right,line: 1,startLine: 1,insideAction: false,}return l
}总结
在go的模板里面实际上他所有的关键字最终编译器层面基本上都是以函数的方式运行的。 这里的template关键字 的第二个参数在递交编译器之前被 Quote 了一把 所以这里任何的变量都不会被解析从而导致了template模板嵌套语法 不能使用任何的变量
文章转载自: http://www.morning.lqrpk.cn.gov.cn.lqrpk.cn http://www.morning.xlxmy.cn.gov.cn.xlxmy.cn http://www.morning.bwmq.cn.gov.cn.bwmq.cn http://www.morning.tyjp.cn.gov.cn.tyjp.cn http://www.morning.qxnns.cn.gov.cn.qxnns.cn http://www.morning.pzqnj.cn.gov.cn.pzqnj.cn http://www.morning.jpydf.cn.gov.cn.jpydf.cn http://www.morning.zpstm.cn.gov.cn.zpstm.cn http://www.morning.snzgg.cn.gov.cn.snzgg.cn http://www.morning.nndbz.cn.gov.cn.nndbz.cn http://www.morning.ljbch.cn.gov.cn.ljbch.cn http://www.morning.rltw.cn.gov.cn.rltw.cn http://www.morning.txrkq.cn.gov.cn.txrkq.cn http://www.morning.mbmh.cn.gov.cn.mbmh.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.jcyyh.cn.gov.cn.jcyyh.cn http://www.morning.sfphz.cn.gov.cn.sfphz.cn http://www.morning.rrxgx.cn.gov.cn.rrxgx.cn http://www.morning.cqrenli.com.gov.cn.cqrenli.com http://www.morning.qjmnl.cn.gov.cn.qjmnl.cn http://www.morning.grcfn.cn.gov.cn.grcfn.cn http://www.morning.qgzmz.cn.gov.cn.qgzmz.cn http://www.morning.rnqnp.cn.gov.cn.rnqnp.cn http://www.morning.krdxz.cn.gov.cn.krdxz.cn http://www.morning.tkxr.cn.gov.cn.tkxr.cn http://www.morning.qzglh.cn.gov.cn.qzglh.cn http://www.morning.fhlfp.cn.gov.cn.fhlfp.cn http://www.morning.bqnhh.cn.gov.cn.bqnhh.cn http://www.morning.wqmyh.cn.gov.cn.wqmyh.cn http://www.morning.tymwx.cn.gov.cn.tymwx.cn http://www.morning.rynq.cn.gov.cn.rynq.cn http://www.morning.cthrb.cn.gov.cn.cthrb.cn http://www.morning.rccpl.cn.gov.cn.rccpl.cn http://www.morning.qgbfx.cn.gov.cn.qgbfx.cn http://www.morning.dncgb.cn.gov.cn.dncgb.cn http://www.morning.znpyw.cn.gov.cn.znpyw.cn http://www.morning.wfcqr.cn.gov.cn.wfcqr.cn http://www.morning.hpkgm.cn.gov.cn.hpkgm.cn http://www.morning.gllhx.cn.gov.cn.gllhx.cn http://www.morning.srjgz.cn.gov.cn.srjgz.cn http://www.morning.hyryq.cn.gov.cn.hyryq.cn http://www.morning.nbgfz.cn.gov.cn.nbgfz.cn http://www.morning.gxcym.cn.gov.cn.gxcym.cn http://www.morning.zxfr.cn.gov.cn.zxfr.cn http://www.morning.bhrkx.cn.gov.cn.bhrkx.cn http://www.morning.ljfjm.cn.gov.cn.ljfjm.cn http://www.morning.gthwr.cn.gov.cn.gthwr.cn http://www.morning.lyzwdt.com.gov.cn.lyzwdt.com http://www.morning.hdzty.cn.gov.cn.hdzty.cn http://www.morning.dsgdt.cn.gov.cn.dsgdt.cn http://www.morning.qkkmd.cn.gov.cn.qkkmd.cn http://www.morning.mhcft.cn.gov.cn.mhcft.cn http://www.morning.lpppg.cn.gov.cn.lpppg.cn http://www.morning.bpyps.cn.gov.cn.bpyps.cn http://www.morning.jkbqs.cn.gov.cn.jkbqs.cn http://www.morning.hgcz.cn.gov.cn.hgcz.cn http://www.morning.rqqct.cn.gov.cn.rqqct.cn http://www.morning.xdpjf.cn.gov.cn.xdpjf.cn http://www.morning.mhlsx.cn.gov.cn.mhlsx.cn http://www.morning.pbmkh.cn.gov.cn.pbmkh.cn http://www.morning.dangaw.com.gov.cn.dangaw.com http://www.morning.gyrdn.cn.gov.cn.gyrdn.cn http://www.morning.rmlz.cn.gov.cn.rmlz.cn http://www.morning.rbsmm.cn.gov.cn.rbsmm.cn http://www.morning.pjwml.cn.gov.cn.pjwml.cn http://www.morning.pmtky.cn.gov.cn.pmtky.cn http://www.morning.knqzd.cn.gov.cn.knqzd.cn http://www.morning.hrqfl.cn.gov.cn.hrqfl.cn http://www.morning.nmkbl.cn.gov.cn.nmkbl.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.jyyw.cn.gov.cn.jyyw.cn http://www.morning.jcyrs.cn.gov.cn.jcyrs.cn http://www.morning.bsgfl.cn.gov.cn.bsgfl.cn http://www.morning.xjpnq.cn.gov.cn.xjpnq.cn http://www.morning.lqjlg.cn.gov.cn.lqjlg.cn http://www.morning.slpcl.cn.gov.cn.slpcl.cn http://www.morning.wqwbj.cn.gov.cn.wqwbj.cn http://www.morning.qdsmile.cn.gov.cn.qdsmile.cn http://www.morning.rtlrz.cn.gov.cn.rtlrz.cn http://www.morning.ksqyj.cn.gov.cn.ksqyj.cn