当前位置:网站首页>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>
边栏推荐
- Create, modify and delete tables
- Srping bean in the life cycle
- 「游戏引擎 浅入浅出」4.1 Unity Shader和OpenGL Shader
- datagrip 报错 “The specified database userpassword combination is rejected...”的解决方法
- flinkcdc对mysql的date字段类型转化有什么解决思路么
- 2022年牛客多校第四场补题
- 滚动条样式修改
- 选择排序—直接选择排序和堆排序
- 【视觉SLAM十四讲】第一章理论详解
- Xiaobai's 0 Basic Tutorial SQL: An Overview of Relational Databases 02
猜你喜欢
随机推荐
小程序通过云函数操作数据库【使用get取数据库】
小白的0基础教程SQL: 关系数据库概述 02
Dialogue with the father of MySQL: One excellent programmer is worth 5 ordinary programmers
升级为重量级锁,锁重入会导致锁释放?
戴尔PowerEdge服务器R450 RAID配置步骤
Introduction to the basic principles, implementation and problem solving of crawler
Win任务栏图标异常解决
datagrip 报错 “The specified database userpassword combination is rejected...”的解决方法
史上超强最常用SQL语句大全
如何使用Photoshop合成星轨照片,夜空星轨照片后期处理方法
matlab 风速模型 小波滤波
ORACLE modify another user package (package)
目标检测概述-上篇
sum of special numbers
2022年牛客多校第四场补题
「游戏引擎 浅入浅出」4.1 Unity Shader和OpenGL Shader
More than 2022 cattle guest school game 4 yue
Dbeaver connect the MySQL database and error Connection refusedconnect processing
LeetCode 0150. Reverse Polish Expression Evaluation
Why is the lightweight VsCode used more and more?Why eat my C drive 10G?How to Painlessly Clean VsCode Cache?Teach you how to lose weight for C drive









