Go语言之自定义模板,修改模板默认变量符号

默认情况下,Go语言引用HTML文件,HTML文件中使用{{ }},使用Delims参数可以修改默认的符号

目录结构:

main.go
index.html

main.go内容

package main

import (
	"fmt"
	"html/template"
	"net/http"
)

func index(w http.ResponseWriter, r *http.Request)  {
//使用Delims参数可修改默认符号
//定义模板
//解析模板
	t,err := template.New("index.tmpl").Delims("{[","]}").ParseFiles("./index.tmpl")
	if err !=nil {
		fmt.Printf("parse template failed,err%v",err)
		return
	}
//渲染模板
	name := "Go语言"
	t.Execute(w,name)

}
func main() {
	http.HandleFunc("/index",index)
	err := http.ListenAndServe(":9000",nil)
	if err !=nil {
		fmt.Printf("HTTP server start failed,err%v",err)
		return
	}

}

index.tmpl内容

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <title>修改默认符号模板</title>
</head>
<body>
{{/*   这里把默认的{{}}改成{[]},与main.go代码中Delims定义的一致   }}*/}}
{[. ]}
</body>
</html>

运行代码:

go run main.go

打开浏览器http://ip:9000/index
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

登云时刻

整理不易,请个辣条吃吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值