Go语言实现完整Web服务器及中文闪卡应用
1. Go模板包基础
在Go语言编程中,模板包在文本转换方面发挥着重要作用。之前的程序多使用 text/template 包,它能进行文本转换,但不考虑文本使用的上下文。例如,在 PrintPerson.go 中,如果文本如下设置:
job1 := Job{Employer: "<script>alert('Could be nasty!')</script>", Role: "Director, Commerce and ICT"}
程序生成的文本会是:
An employer is <script>alert('Could be nasty!')</script>
若将此文本下载到浏览器,会产生意外效果。可以在管道中使用 html 命令来减少这种情况,如 {
{. | html}} ,会生成如下内容:
An employer is <script>alert('Could be nasty!')</script>
不过,对所有表达式应用此过滤器会很繁琐,而且可能无法捕获潜在危险的JavaScript、CSS或URI表达式。
超级会员免费看
订阅专栏 解锁全文
65

被折叠的 条评论
为什么被折叠?



