当前位置:网站首页>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>
边栏推荐
猜你喜欢

响应式织梦模板园林景观类网站

MVVM项目开发(商品管理系统一)

从零开始—仿牛客网讨论社区项目(一)

mysql中添加字段的相关问题

LeetCode 0149. Maximum number of points on a line

Matlab simulink particle swarm optimization fuzzy pid control motor pump

企业员工人事管理系统(数据库课设)

Using FiddlerScript caught poly FiddlerScript 】 【 download

安装SQL Server详细教程

I have three degrees, and I have five faces. I was "confessed" by the interviewer, and I got an offer of 33*15.
随机推荐
小程序通过云函数操作数据库【使用get取数据库】
阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
小白的0基础教程SQL: 什么是SQL 01
rhcsa 第三次
2022杭电多校第二场1011 DOS Card(线段树)
Win任务栏图标异常解决
太厉害了,终于有人能把文件上传漏洞讲的明明白白了
从底层结构开始学习FPGA(6)----分布式RAM(DRAM,Distributed RAM)
The Bean's life cycle
Jupyter shortcuts
小白的0基础教程SQL: 关系数据库概述 02
数据湖:数据同步工具NiFi
NIO programming
How JS works
选择排序—直接选择排序和堆排序
「游戏引擎 浅入浅出」4.1 Unity Shader和OpenGL Shader
阿里三面:MQ 消息丢失、重复、积压问题,该如何解决?
JS的运行原理
Go 支持 OOP: 用 struct 代替 class
数据机构----线性表之单向链表