package mainimport ("net/http""github.com/gin-gonic/gin")func main() {// 创建一个默认的路由引擎r := gin.Default()//配置路由r.GET("/", func(c *gin.Context) {c.String(http.StatusOK, "值:%v", "你好gin")})r.GET("/news", func(c *gin.Context) {c.String(http.StatusOK, "我是新闻页面 111")})r.POST("/add", func(c *gin.Context) {c.String(http.StatusOK, "这是一个post--主要用于增加数据")})r.PUT("/edit", func(c *gin.Context) {c.String(200, "这是一个put请求 主要用于编辑数据")})r.DELETE("/delete", func(c *gin.Context) {c.String(200, "这是一个DELETE请求 用于删除数据")})// r.Run() 启动 HTTP 服务,默认在 0.0.0.0:8080 启动服务r.Run(":8000") //启动一个web服务}
package mainimport ("net/http""github.com/gin-gonic/gin")type Article struct {Title string `json:"title"`Desc string `json:"desc"`Content string `json:"content"`}func main() {r := gin.Default()//配置模板的文件r.LoadHTMLGlob("templates/*")r.GET("/", func(c *gin.Context) {c.String(200, "值:%v", "首页")})r.GET("/json1", func(c *gin.Context) {c.JSON(200, map[string]interface{}{"success": true,"msg": "你好gin",})})r.GET("/json2", func(c *gin.Context) {c.JSON(200, gin.H{"success": true,"msg": "你好gin--22",})})r.GET("/json3", func(c *gin.Context) {a := &Article{Title: "我是一个标题",Desc: "描述",Content: "测试内容",}c.JSON(200, a)})//响应Jsonp请求// http://localhost:8080/jsonp?callback=xxxx// xxxx({"title":"我是一个标题-jsonp","desc":"描述","content":"测试内容"});r.GET("/jsonp", func(c *gin.Context) {a := &Article{Title: "我是一个标题-jsonp",Desc: "描述",Content: "测试内容",}c.JSONP(200, a)})r.GET("/xml", func(c *gin.Context) {c.XML(http.StatusOK, gin.H{"success": true,"msg": "你好gin 我是一个xml",})})r.GET("/news", func(c *gin.Context) {//r.LoadHTMLGlob("templates/*")c.HTML(http.StatusOK, "news.html", gin.H{"title": "我是后台的数据",})})r.GET("/goods", func(c *gin.Context) {c.HTML(http.StatusOK, "goods.html", gin.H{"title": "我是商品页面","price": 20,})})r.Run()}
<!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>{{.title}}</h2><br>{{.price}}<br><h5>html的数据</h5></body></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>{{.title}}</h2></body></html>
package mainimport ("net/http""github.com/gin-gonic/gin")type Article struct {Title stringContent string}func main() {r := gin.Default()//加载模板r.LoadHTMLGlob("templates/*")r.GET("/", func(c *gin.Context) {c.HTML(http.StatusOK, "index.html", gin.H{"title": "首页",})})r.GET("/news", func(c *gin.Context) {news := &Article{Title: "新闻标题",Content: "新闻详情",}c.HTML(http.StatusOK, "news.html", gin.H{"title": "新闻页面","news": news,})})r.Run()}
<!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>{{.title}}</h2></body></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>{{.title}}</h2><p>{{.news.Title}}</p><p>{{.news.Content}}</p></body></html>
package mainimport ("net/http""github.com/gin-gonic/gin")type Article struct {Title stringContent string}func main() {r := gin.Default()//加载模板 放在配置路由上面r.LoadHTMLGlob("templates/**/*")//前台r.GET("/", func(c *gin.Context) {c.HTML(http.StatusOK, "default/index.html", gin.H{"title": "首页","msg": " 我是msg","score": 89,"hobby": []string{"吃饭", "睡觉", "写代码"},"newsList": []interface{}{&Article{Title: "新闻标题111",Content: "新闻详情111",},&Article{Title: "新闻标题222",Content: "新闻详情222",},},"testSlice": []string{},"news": &Article{Title: "新闻标题",Content: "新闻内容",},})})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()}
{{ 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></head><body><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}}</body></html>{{ end }}
<!-- 相当于给模板定义一个名字 define end 成对出现-->{{ define "default/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>{{.title}}</h2><p>{{.news.Title}}</p><p>{{.news.Content}}</p></body></html>{{end}}
package mainimport ("fmt""html/template""net/http""time""github.com/gin-gonic/gin")type Article struct {Title stringContent 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()}
<!-- 相当于给模板定义一个名字 define end 成对出现-->{{ define "public/page_footer.html" }}<h1>我是一个公共的底部</h1>{{end}}
<!-- 相当于给模板定义一个名字 define end 成对出现-->{{ define "public/page_header.html" }}<h1>我是一个公共的标题---{{.title}}</h1>{{end}}
{{ 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>{{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 }}
<!-- 相当于给模板定义一个名字 define end 成对出现-->{{ define "default/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><link rel="stylesheet" href="static/css/base.css"></head><body>{{template "public/page_header.html" .}}<h2>{{.title}}</h2><p>{{.news.Title}}</p><p>{{.news.Content}}</p><br><br>{{template "public/page_footer.html" .}}</body></html>{{end}}
版权声明
本站仅做备份收录,仅供研究与教学参考之用。
读者将信息用于其他用途的,全部法律及连带责任由读者自行承担,本站不承担任何责任。









评论