template模板
一、template模板案例
1、解析字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package main import ( "log" "os" "text/template" ) func main() { //Default返回一个默认的路由引擎 txt := `{{ . }}` data := "james" //创建模板 tpl := template.New( "txtTemplate" ) tpl, err := tpl.Parse(txt) if err != nil { log.Fatal(err) } //执行输出到控制台 err = tpl.Execute(os.Stdout, data) //fmt.Println(err) } |
2、携带if else
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | func main() { //Default返回一个默认的路由引擎 txt := `{{ if . }} true {{ else }} 无内容 {{end}} ` data := true //创建模板 tpl := template.New( "txtTemplate" ) tpl, err := tpl.Parse(txt) if err != nil { log.Fatal(err) } //执行输出到控制台 err = tpl.Execute(os.Stdout, data) //fmt.Println(err) }<br> //输出true |
3、index索引
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | func main() { //Default返回一个默认的路由引擎 txt := `{{ index . 2}}` data := []int{2, 2, 3, 4} //创建模板 tpl := template.New( "txtTemplate" ) tpl, err := tpl.Parse(txt) if err != nil { log.Fatal(err) } //执行输出到控制台 err = tpl.Execute(os.Stdout, data) //fmt.Println(err) } |
4、range遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package main import ( "log" "os" "text/template" ) func main() { //Default返回一个默认的路由引擎 //range 中,表示遍历的每一个元素,输出的是值 txt := `{{ range . }} {{ . }} {{ end }} ` //输出k,v txt1 :=`{{ range $i,$v := . }} {{ $i }}: {{ $v }} {{ end }} ` data := []int{2, 2, 3, 4} //创建模板 tpl := template.New( "txtTemplate" ) tpl, err := tpl.Parse(txt) if err != nil { log.Fatal(err) } //执行输出到控制台 err = tpl.Execute(os.Stdout, data) //fmt.Println(err) } |
5、对结构体操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package main import ( "log" "os" "text/template" ) type User struct { ID string Name string } func main() { //Default返回一个默认的路由引擎 txt := ` <table> <thead> <tr> <th>ID</th> <th>Name</th> </tr> </thead> <tbody> {{ range . }} <tr> <td>{{ .ID }}</td> <td>{{ .Name }}</td> </tr> </tbody> </table> {{ end }} ` data := []User{{ "1" , "小明" }, { "2" , "小李" }} //创建模板 tpl := template.New( "txtTemplate" ) tpl, err := tpl.Parse(txt) if err != nil { log.Fatal(err) } //执行输出到控制台 err = tpl.Execute(os.Stdout, data) //fmt.Println(err) } |
二、模板嵌套
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package main import ( "os" "text/template" ) type title struct { Title string } func main(){ header :=`{{ define "header" }}<head><meta charset= "utf-8" /><title>{{ .Title }}</title></head>{{ end }}` page1 :=`{{ define "page1" }}<!DOCYPE html> <html> {{ template "header" . }} <body> this is page1 </body> </html> {{ end }} ` page2 :=`{{ define "page2" }}<!DOCYPE html> <html> {{ template "header" . }} <body> this is page2 </body> </html> {{ end }} ` var ti1 =title{ "定义page1" } tp1,_ :=template.New( "tp1" ).Parse(header) tp1,_ = tp1.Parse(page1) tp1,_ = tp1.Parse(page2) tp1.ExecuteTemplate(os.Stdout, "page1" ,ti1) } |
三、分开模板
1、page.html文件
1 2 3 4 5 6 7 | <!DOCYPE html> <html> {{ template "header.html" . }} //文件名称进行引用 <body> this is page2 </body> </html> |
2、header.html文件
1 | <head><meta charset= "utf-8" /><title>{{ .Title }}</title></head> |
3、go文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package main import ( "html/template" "os" ) type User struct { ID string Name string } func main(){ tpl,_ :=template.New( "tpl" ).ParseFiles( "tpl/header.html" , "tpl/page.html" ) tpl.ExecuteTemplate(os.Stdout, "page.html" , struct { Ttile string }{ "模板文件" }) |
四、go文件内书写html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package main import ( "fmt" "net/http" "text/template" ) func main() { http.HandleFunc( "/header/" , func (w http.ResponseWriter, r *http.Request) { tpl, err := template.ParseFiles( "./HTML/index.html" ) if err != nil { panic(err) } err = tpl.ExecuteTemplate(w, "index.html" , nil) if err != nil { panic(err) } fmt.Println( "------------------" ) }) http.ListenAndServe( ":9099" , nil) } |
感谢您的阅读,如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮。本文欢迎各位转载,但是转载文章之后必须在文章页面中给出作者和原文连接。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫