main.go
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | package main import ( "fmt" "html/template" "net/http" "time" "github.com/gin-gonic/gin" ) type Article struct { Title string Content string } //时间戳转换成日期 func UnixToTime(timestamp int) string { fmt.Println(timestamp) t := time.Unix(int64(timestamp), 0) return t.Format( "2006-01-02 15:04:05" ) } func Println(str1 string, str2 string) string { fmt.Println(str1, str2) return str1 + "----" + str2 } func main() { // 创建一个默认的路由引擎 r := gin.Default() //自定义模板函数 注意要把这个函数放在加载模板前 r.SetFuncMap(template.FuncMap{ "UnixToTime" : UnixToTime, "Println" : Println, }) //加载模板 放在配置路由上面 r.LoadHTMLGlob( "templates/**/*" ) //配置静态web目录 第一个参数表示路由, 第二个参数表示映射的目录 r.Static( "/static" , "./static" ) //前台 r.GET( "/" , func (c *gin.Context) { c.HTML(http.StatusOK, "default/index.html" , gin.H{ "title" : "aaa" , "msg" : " 我是msg" , "score" : 89, "hobby" : []string{ "吃饭" , "睡觉" , "写代码" }, "newsList" : [] interface {}{ &Article{ Title: "新闻标题111" , Content: "新闻详情111" , }, &Article{ Title: "新闻标题222" , Content: "新闻详情222" , }, }, "testSlice" : []string{}, "news" : &Article{ Title: "新闻标题" , Content: "新闻内容" , }, "date" : 1629423555, }) }) r.GET( "/news" , func (c *gin.Context) { news := &Article{ Title: "新闻标题" , Content: "新闻详情" , } c.HTML(http.StatusOK, "default/news.html" , gin.H{ "title" : "新闻页面" , "news" : news, }) }) //后台 r.GET( "/admin" , func (c *gin.Context) { c.HTML(http.StatusOK, "admin/index.html" , gin.H{ "title" : "后台首页" , }) }) r.GET( "/admin/news" , func (c *gin.Context) { c.HTML(http.StatusOK, "admin/news.html" , gin.H{ "title" : "新闻页面" , }) }) - r.Run() } |
templates->default->index.html
<!-- 相当于给模板定义一个名字 define end 成对出现-->
{{ define "default/index.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>
<link rel="stylesheet" href="/static/css/base.css">
</head>
<body>
{{template "public/page_header.html" .}}
<img src="/static/images/node.jpg" alt="">
<h2>{{.title}}</h2>
<!-- 定义变量 -->
{{$t := .title}}
<br>
<h4>
{{$t}}
</h4>
<!-- 条件判断 -->
{{if ge .score 60}}
<p>及格</p>
{{else}}
<p>不及格</p>
{{end}}
{{if gt .score 90}}
<p>优秀</p>
{{else if gt .score 80}}
<p>良好</p>
{{else if gt .score 60}}
<p>及格</p>
{{else}}
<p>不及格</p>
{{end}}
<!-- 循环遍历数据 -->
<ul>
{{range $key,$value:=.hobby}}
<li>{{$key}}----{{$value}}</li>
{{end}}
</ul>
<br>
<ul>
{{range $key,$value:=.newsList}}
<li>{{$key}}----{{$value.Title}}---{{$value.Content}}</li>
{{end}}
</ul>
<br>
<ul>
{{range $key,$value:=.testSlice}}
<li>{{$key}}----{{$value}}</li>
{{else}}
<li>数组中没有数据</li>
{{end}}
</ul>
<!-- with 解构结构体 -->
<p>{{.news.Title}}</p>
<p>{{.news.Content}}</p>
<br>
{{with .news}}
<p>{{.Title}}</p>
<p>{{.Content}}</p>
{{end}}
<br>
<!-- 预定义函数 (了解) -->
{{len .title}}
<br>
<br>
<!-- 自定义模板函数 -->
{{.date}}
<br>
<br>
{{UnixToTime .date}}
<br>
<br>
{{Println .title .msg}}
<br> <br>
{{template "public/page_footer.html" .}}
</body>
</html>
{{ end }}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?