当前位置:网站首页>The use of Golang: go template engine
The use of Golang: go template engine
2022-08-01 07:03:00 【ZzzWClock】
go模版引擎的使用
一.前言
import “html/template”
template包(html/template)实现了数据驱动的模板,用于生成可对抗代码注入的安全HTML输出.本包提供了和text/template包相同的接口,无论何时当输出是HTML的时候都应使用本包.
二.示例
- 使用html/templateParse the template file and pass parameters to the template file

- golang代码块
package mian
import (
"fmt"
"html/template"
"net/http"
)
// html处理器
func htmlHandle(w http.ResponseWriter, r *http.Request) {
// Parse a template file
// MustFunctions are used to wrap returns(*Template, error)的函数/方法调用,它会在err非nil时panic,Generally used for variable initialization:
// ParseFilesThe function creates a template and parses itfilenamesTemplate definitions in the specified file.The name of the returned template is the filename of the first file(不含扩展名),The content is the content of the first parsed file.至少要提供一个文件.如果发生错误,会停止解析并返回nil.
t := template.Must(template.ParseFiles("index.html"))
// Execute方法将解析好的模板应用到data上,并将输出写入wr.如果执行时出现错误,会停止执行,但有可能已经写入wr部分数据.模板可以安全的并发执行.
t.Execute(w, "I am a passage of words")
}
func main() {
// 路由器
http.HandleFunc("/html", htmlHandle)
// 开启server服务
http.ListenAndServe(":8081", nil)
}
- html代码块
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p style="color: red;">I am a template file:{
{.}}</p>
</body>
</html>
边栏推荐
- 从离线到实时对客,湖仓一体释放全量数据价值
- 测试工具(四)Jenkins环境搭建与使用
- JVM:运行时数据区-PC寄存器(程序计数器)
- Solve the problem of page flicker caused by browser scroll bars
- ORACLE modify another user package (package)
- Bean的生命周期
- Matlab simulink particle swarm optimization fuzzy pid control motor pump
- 第6章——数据库的安全性
- 信息系统项目管理师必背核心考点(五十六)配置控制委员会(CCB)的工作
- 上课作业(7)——#598. 取余运算(mod)
猜你喜欢

【FiddlerScript】利用FiddlerScript抓包保利威下载

Introduction to the basic principles, implementation and problem solving of crawler

rhcsa 第四天

曲柄滑块机构运动分析和参数优化

Information system project managers must recite the work of the core test site (56) Configuration Control Board (CCB)

阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外

JS的运行原理

如何使用Photoshop合成星轨照片,夜空星轨照片后期处理方法

安装SQL Server详细教程

datagrip 报错 “The specified database userpassword combination is rejected...”的解决方法
随机推荐
return;代表含义
sum of special numbers
Image lossless compression software which works: try completely free JPG - C image batch finishing compression reduces weight tools | latest JPG batch dressing tools download
小白的0基础教程SQL: 什么是SQL 01
深度比较两个对象是否相同
JVM:运行时数据区-PC寄存器(程序计数器)
MVVM project development (commodity management system 1)
监听父元素宽高,自适应插件大小
特殊的日子,值得纪念
曲柄滑块机构运动分析和参数优化
2022.7.26 Mock Competition
leetcode125 Verify palindrome string
Guest brush SQL - 2
滚动条样式修改
How JS works
LeetCode 0150. 逆波兰表达式求值
图片无损压缩软件哪个好用:试试完全免费的JPG-C 图片批量修整压缩减肥工具吧 | 最新jpg批量修整工具下载
The BP neural network based on MATLAB voice characteristic signal classification
Dart exception details
问下 mysql向pg同步多个表的话 有什么好的方案吗?