package mainimport ( "fmt" "html/template" "time" "github.com/gin-gonic/gin")//时间戳转换成日期func UnixToTime(timestamp int) string { fmt.Println(timestamp) t := time.Unix(int64(timestamp), 0) return t.Format("2006-01-02 15:04:05")}func main() { // 创建一个默认的路由引擎 r := gin.Default() //自定义模板函数 注意要把这个函数放在加载模板前 r.SetFuncMap(template.FuncMap{ "UnixToTime": UnixToTime, }) //加载模板 放在配置路由前面 r.LoadHTMLGlob("templates/**/*") //配置静态web目录 第一个参数表示路由, 第二个参数表示映射的目录 r.Static("/static", "./static") defaultRouters := r.Group("/") { defaultRouters.GET("/", func(c *gin.Context) { c.String(200, "首页") }) defaultRouters.GET("/news", func(c *gin.Context) { c.String(200, "新闻") }) } apiRouters := r.Group("/api") { apiRouters.GET("/", func(c *gin.Context) { c.String(200, "我是一个api接口") }) apiRouters.GET("/userlist", func(c *gin.Context) { c.String(200, "我是一个api接口-userlist") }) apiRouters.GET("/plist", func(c *gin.Context) { c.String(200, "我是一个api接口-plist") }) } adminRouters := r.Group("/admin") { adminRouters.GET("/", func(c *gin.Context) { c.String(200, "后台首页") }) adminRouters.GET("/user", func(c *gin.Context) { c.String(200, "用户列表") }) adminRouters.GET("/article", func(c *gin.Context) { c.String(200, "新闻列表") }) } r.Run()}
{{ define "admin/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></head><body> <h2>这是后台首页</h2></body></html>{{ end }}
{{ define "admin/news.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> <h2>后台新闻页面</h2></body></html>{{ end }}
<!-- 相当于给模板定义一个名字 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> <h1>Post演示</h1></body></html>{{ end }}
<!-- 相当于给模板定义一个名字 define end 成对出现-->{{ define "default/user.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> <form action="/doAddUser2" method="post"> 用户名:<input type="text" name="username" /> <br><br> 密码:<input type="password" name="password" /> <br><br> <input type="submit" value="提交"> </form></body></html>{{end}}
<!-- 相当于给模板定义一个名字 define end 成对出现-->{{ define "public/page_footer.html" }} <h1> 我是一个公共的底部 </h1>{{end}}
<!-- 相当于给模板定义一个名字 define end 成对出现-->{{ define "public/page_header.html" }} <h1> 我是一个公共的标题---{{.title}} </h1>{{end}}
package mainimport ( "fmt" "gindemo06/routers" "html/template" "time" "github.com/gin-gonic/gin")//时间戳转换成日期func UnixToTime(timestamp int) string { fmt.Println(timestamp) t := time.Unix(int64(timestamp), 0) return t.Format("2006-01-02 15:04:05")}func main() { // 创建一个默认的路由引擎 r := gin.Default() //自定义模板函数 注意要把这个函数放在加载模板前 r.SetFuncMap(template.FuncMap{ "UnixToTime": UnixToTime, }) //加载模板 放在配置路由前面 r.LoadHTMLGlob("templates/**/*") //配置静态web目录 第一个参数表示路由, 第二个参数表示映射的目录 r.Static("/static", "./static") routers.AdminRoutersInit(r) routers.ApiRoutersInit(r) routers.DefaultRoutersInit(r) r.Run()}
package routersimport "github.com/gin-gonic/gin"func AdminRoutersInit(r *gin.Engine) { adminRouters := r.Group("/admin") { adminRouters.GET("/", func(c *gin.Context) { c.String(200, "后台首页") }) adminRouters.GET("/user", func(c *gin.Context) { c.String(200, "用户列表") }) adminRouters.GET("/article", func(c *gin.Context) { c.String(200, "新闻列表") }) }}
package routersimport "github.com/gin-gonic/gin"func ApiRoutersInit(r *gin.Engine) { apiRouters := r.Group("/api") { apiRouters.GET("/", func(c *gin.Context) { c.String(200, "我是一个api接口") }) apiRouters.GET("/userlist", func(c *gin.Context) { c.String(200, "我是一个api接口-userlist") }) apiRouters.GET("/plist", func(c *gin.Context) { c.String(200, "我是一个api接口-plist") }) }}
package routersimport ( "net/http" "github.com/gin-gonic/gin")func DefaultRoutersInit(r *gin.Engine) { defaultRouters := r.Group("/") { defaultRouters.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "default/index.html", gin.H{ "msg": "我是一个msg", }) }) defaultRouters.GET("/news", func(c *gin.Context) { c.String(200, "新闻") }) }}
{{ define "admin/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></head><body> <h2>这是后台首页</h2></body></html>{{ end }}
{{ define "admin/news.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> <h2>后台新闻页面</h2></body></html>{{ end }}
<!-- 相当于给模板定义一个名字 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> <h1>我是一个首页</h1> <br> {{.msg}}</body></html>{{ end }}
<!-- 相当于给模板定义一个名字 define end 成对出现-->{{ define "default/user.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> <form action="/doAddUser2" method="post"> 用户名:<input type="text" name="username" /> <br><br> 密码:<input type="password" name="password" /> <br><br> <input type="submit" value="提交"> </form></body></html>{{end}}
<!-- 相当于给模板定义一个名字 define end 成对出现-->{{ define "public/page_footer.html" }} <h1> 我是一个公共的底部 </h1>{{end}}
<!-- 相当于给模板定义一个名字 define end 成对出现-->{{ define "public/page_header.html" }} <h1> 我是一个公共的标题---{{.title}} </h1>{{end}}
源码.7z