当前位置:网站首页>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>
边栏推荐
- Solve the problem of page flicker caused by browser scroll bars
- CSP-S2019兴奋不已
- Leetcode第 304 场周赛
- Dialogue with the father of MySQL: One excellent programmer is worth 5 ordinary programmers
- weight distribution
- 史上超强最常用SQL语句大全
- 第6章——数据库的安全性
- Go 支持 OOP: 用 struct 代替 class
- Introduction to the basic principles, implementation and problem solving of crawler
- 旋度(7)连接失败localhost8080;连接拒绝了
猜你喜欢

curl (7) Failed connect to localhost8080; Connection refused

湖仓一体电商项目(一):项目背景和架构介绍

奇葩问题 npm install 报错 gyp ERR

对于升级go1.18的goland问题

Srping中bean的生命周期

插入排序—直接插入排序和希尔排序

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

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

Offer刷题——1

第5章——以程序方式处理MySQL数据表的数据
随机推荐
声音信号处理基频检测和时频分析
Dell PowerEdge Server R450 RAID Configuration Steps
对于升级go1.18的goland问题
爬虫框架 Scrapy 详解
Talk about the bugs in using for in to traverse the array in js
「面经分享」西北大学 | 字节 生活服务 | 一面二面三面 HR 面
Data organization -- singly linked list of the linear table
特殊的日子,值得纪念
我说过无数遍了:从来没有一种技术是为灵活组合这个目标而设计的
第02章 MySQL的数据目录【1.MySQL架构篇】【MySQL高级】
奇葩问题 npm install 报错 gyp ERR
第5章——以程序方式处理MySQL数据表的数据
LeetCode每日一题(309. Best Time to Buy and Sell Stock with Cooldown)
LeetCode Question of the Day (309. Best Time to Buy and Sell Stock with Cooldown)
Vsce package after the Command failed: NPM list - production - parseable - the depth = 99999 - loglevel = error exception
LeetCode 0149. Maximum number of points on a line
Srping bean in the life cycle
matlab 风速模型 小波滤波
05-SDRAM: Arbitration
rhcsa 第四天