Golang Gin 初识
1.Gin框架搭建,初识
1 // main.go 2 package main 3 4 /*** 5 Gin入门: 6 文档: https://gin-gonic.com/zh-cn/docs/quickstart/ 7 热加载: 8 文档: https://github.com/cosmtrek/air/blob/master/README-zh_cn.md 9 安装: go install github.com/cosmtrek/air@latest 10 配置: alias air='/home/chad/golang/workspace/bin/air' 11 初始化: 1. cd yourworkspace 12 2. air -c .air.toml 13 3. air init 14 4. air 15 ***/ 16 17 import ( 18 "net/http" 19 20 "github.com/gin-gonic/gin" 21 ) 22 23 // 快速入门 24 func main1() { 25 // 创建默认路由引擎 26 r := gin.Default() 27 28 // 配置路由 29 r.GET("/", func(c *gin.Context) { 30 c.String(http.StatusOK, "Value:%v", "Hello word!") 31 }) 32 33 r.GET("/new", func(c *gin.Context) { 34 c.String(http.StatusOK, "Value:%v", "我是新页面!") 35 }) 36 37 r.GET("/new1", func(c *gin.Context) { 38 c.String(http.StatusOK, "Value:%v", "我是新页面1!") 39 }) 40 41 r.POST("/add", func(c *gin.Context) { 42 c.String(http.StatusOK, "我是POST请求:%v", "主要用来新增数据!") 43 }) 44 45 r.PUT("/edit", func(c *gin.Context) { 46 c.String(http.StatusOK, "我是PUT请求:%v", "主要用来编辑数据!") 47 }) 48 49 r.DELETE("/delete", func(c *gin.Context) { 50 c.String(http.StatusOK, "我是DELETE请求:%v", "主要用来删除数据!") 51 }) 52 r.Run("0.0.0.0:8000") // 默认监听 0.0.0.0:8080 上启动服务 53 } 54 55 func main() { 56 TestResponseHTML() // 返回HTML 57 // TestResponseJsonXML() // 返回Json丶Xml数据 58 } 59 60 type UserInfo struct { 61 UserID string `json:"userid"` 62 UserName string 63 Age int 64 } 65 66 type Article struct { 67 Title string `json:"title"` 68 Content string `json:"content"` 69 } 70 71 // TestResponseHTML 返回HTML 72 func TestResponseHTML() { 73 router := gin.Default() 74 // 配置模板路径 75 router.LoadHTMLGlob("templates/*") 76 // router.LoadHTMLFiles("templates/template1.html", "templates/template2.html", ) 77 78 router.GET("/news", func(c *gin.Context) { 79 data := &Article{ 80 Title: "新闻标题", 81 Content: "新闻内容", 82 } 83 c.HTML(http.StatusOK, "news.html", gin.H{ 84 "title": "我是后台数据", 85 "news": data, 86 }) 87 }) 88 89 router.Run("0.0.0.0:8080") 90 91 } 92 93 // 如何返回Json丶Xml数据 94 func TestResponseJsonXML() { 95 r := gin.Default() 96 97 r.GET("/", func(c *gin.Context) { 98 c.String(http.StatusOK, "首页") 99 }) 100 101 r.GET("/json", func(c *gin.Context) { 102 c.JSON(http.StatusOK, map[string]interface{}{ 103 "success": true, 104 "msg": "你好gin.", 105 }) 106 }) 107 108 r.GET("/json2", func(c *gin.Context) { 109 c.JSON(http.StatusOK, gin.H{ 110 "success": true, 111 "msg": "你好gin. --22", 112 }) 113 }) 114 115 r.GET("/json3", func(c *gin.Context) { 116 us := &UserInfo{ 117 UserID: "100", 118 UserName: "zhangsan", 119 Age: 18, 120 } 121 c.JSON(http.StatusOK, us) 122 }) 123 124 // jsonp 主要用来解决跨域问题 125 // http://localhost:8080/jsonp?callback=xxx 126 // xxx({"userid":"100","UserName":"zhangsan","Age":18}); 127 r.GET("/jsonp", func(c *gin.Context) { 128 us := &UserInfo{ 129 UserID: "100", 130 UserName: "zhangsan", 131 Age: 18, 132 } 133 c.JSONP(http.StatusOK, us) 134 }) 135 136 r.GET("/xml", func(c *gin.Context) { 137 // gin.H = map[string]interface{} 138 c.XML(http.StatusOK, map[string]interface{}{ 139 "success": true, 140 "msg": "你好gin.", 141 }) 142 }) 143 144 r.GET("/xml2", func(c *gin.Context) { 145 us := &UserInfo{ 146 UserID: "100", 147 UserName: "zhangsan", 148 Age: 18, 149 } 150 c.XML(http.StatusOK, us) 151 }) 152 r.Run("0.0.0.0:8080") 153 }
2.HTML渲染
1 // templates/news.html 2 <!DOCTYPE html> 3 <html lang="en"> 4 5 <head> 6 <meta charset="UTF-8"> 7 <meta name="viewport" content="width=<device-width>, initial-scale=1.0"> 8 <title>Document</title> 9 </head> 10 11 <body> 12 <h2>我是首页 </h2> 13 <h2>{{.title}} </h2> 14 <hr> 15 <p>{{.news.Title}}</p> 16 <p>{{.news.Content}}</p> 17 </body> 18 19 </html>