1. package main
    2. import (
    3. "encoding/xml"
    4. "fmt"
    5. "html/template"
    6. "net/http"
    7. "time"
    8. "github.com/gin-gonic/gin"
    9. )
    10. type UserInfo struct {
    11. Username string `json:"username" form:"username"`
    12. Password string `json:"password" form:"password"`
    13. }
    14. type Article struct {
    15. Title string `json:"title" xml:"title"`
    16. Content string `json:"content" xml:"content"`
    17. }
    18. //时间戳转换成日期
    19. func UnixToTime(timestamp int) string {
    20. fmt.Println(timestamp)
    21. t := time.Unix(int64(timestamp), 0)
    22. return t.Format("2006-01-02 15:04:05")
    23. }
    24. func main() {
    25. // 创建一个默认的路由引擎
    26. r := gin.Default()
    27. //自定义模板函数 注意要把这个函数放在加载模板前
    28. r.SetFuncMap(template.FuncMap{
    29. "UnixToTime": UnixToTime,
    30. })
    31. //加载模板 放在配置路由前面
    32. r.LoadHTMLGlob("templates/**/*")
    33. //配置静态web目录 第一个参数表示路由, 第二个参数表示映射的目录
    34. r.Static("/static", "./static")
    35. //Get 请求传值
    36. r.GET("/", func(c *gin.Context) {
    37. username := c.Query("username")
    38. age := c.Query("age")
    39. page := c.DefaultQuery("page", "1")
    40. c.JSON(http.StatusOK, gin.H{
    41. "username": username,
    42. "age": age,
    43. "page": page,
    44. })
    45. })
    46. //Get 请求传值 id
    47. r.GET("/article", func(c *gin.Context) {
    48. id := c.DefaultQuery("id", "1")
    49. c.JSON(http.StatusOK, gin.H{
    50. "msg": "新闻详情",
    51. "id": id,
    52. })
    53. })
    54. //post演示
    55. r.GET("/user", func(c *gin.Context) {
    56. c.HTML(http.StatusOK, "default/user.html", gin.H{})
    57. })
    58. //获取表单post过来的数据
    59. r.POST("/doAddUser1", func(c *gin.Context) {
    60. username := c.PostForm("username")
    61. password := c.PostForm("password")
    62. age := c.DefaultPostForm("age", "20")
    63. c.JSON(http.StatusOK, gin.H{
    64. "username": username,
    65. "password": password,
    66. "age": age,
    67. })
    68. })
    69. //获取 GET POST 传递的数据绑定到结构体
    70. //http://localhost:8080/getUser?username=zhangsan&password=1111
    71. r.GET("/getUser", func(c *gin.Context) {
    72. user := &UserInfo{}
    73. if err := c.ShouldBind(&user); err == nil {
    74. fmt.Printf("%#v", user)
    75. c.JSON(http.StatusOK, user)
    76. } else {
    77. c.JSON(http.StatusOK, gin.H{
    78. "err": err.Error(),
    79. })
    80. }
    81. })
    82. r.POST("/doAddUser2", func(c *gin.Context) {
    83. user := &UserInfo{}
    84. if err := c.ShouldBind(&user); err == nil {
    85. c.JSON(http.StatusOK, user)
    86. } else {
    87. c.JSON(http.StatusBadRequest, gin.H{
    88. "err": err.Error(),
    89. })
    90. }
    91. })
    92. //获取 Post Xml 数据
    93. r.POST("/xml", func(c *gin.Context) {
    94. article := &Article{}
    95. xmlSliceData, _ := c.GetRawData() //获取 c.Request.Body 读取请求数据
    96. fmt.Println(xmlSliceData)
    97. if err := xml.Unmarshal(xmlSliceData, &article); err == nil {
    98. c.JSON(http.StatusOK, article)
    99. } else {
    100. c.JSON(http.StatusBadRequest, gin.H{
    101. "err": err.Error(),
    102. })
    103. }
    104. })
    105. // 动态路由传值
    106. // list/123 list/456
    107. r.GET("/list/:cid", func(c *gin.Context) {
    108. cid := c.Param("cid")
    109. c.String(200, "%v", cid)
    110. })
    111. //后台
    112. r.GET("/admin", func(c *gin.Context) {
    113. c.HTML(http.StatusOK, "admin/index.html", gin.H{
    114. "title": "后台首页",
    115. })
    116. })
    117. r.Run()
    118. }
    1. {{ define "admin/index.html" }}
    2. <!DOCTYPE html>
    3. <html lang="en">
    4. <head>
    5. <meta charset="UTF-8">
    6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    8. <title>Document</title>
    9. </head>
    10. <body>
    11. <h2>这是后台首页</h2>
    12. </body>
    13. </html>
    14. {{ end }}
    1. {{ define "admin/news.html" }}
    2. <!DOCTYPE html>
    3. <html lang="en">
    4. <head>
    5. <meta charset="UTF-8">
    6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    8. <title>Document</title>
    9. </head>
    10. <body>
    11. <h2>后台新闻页面</h2>
    12. </body>
    13. </html>
    14. {{ end }}
    1. <!-- 相当于给模板定义一个名字 define end 成对出现-->
    2. {{ define "default/index.html" }}
    3. <!DOCTYPE html>
    4. <html lang="en">
    5. <head>
    6. <meta charset="UTF-8">
    7. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    8. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    9. <title>Document</title>
    10. <link rel="stylesheet" href="/static/css/base.css">
    11. </head>
    12. <body>
    13. <h1>Post演示</h1>
    14. </body>
    15. </html>
    16. {{ end }}
    1. <!-- 相当于给模板定义一个名字 define end 成对出现-->
    2. {{ define "default/user.html" }}
    3. <!DOCTYPE html>
    4. <html lang="en">
    5. <head>
    6. <meta charset="UTF-8">
    7. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    8. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    9. <title>Document</title>
    10. <link rel="stylesheet" href="static/css/base.css">
    11. </head>
    12. <body>
    13. <form action="/doAddUser2" method="post">
    14. 用户名:<input type="text" name="username" /> <br><br>
    15. 密码:<input type="password" name="password" /> <br><br>
    16. <input type="submit" value="提交">
    17. </form>
    18. </body>
    19. </html>
    20. {{end}}
    1. <!-- 相当于给模板定义一个名字 define end 成对出现-->
    2. {{ define "public/page_footer.html" }}
    3. <h1>
    4. 我是一个公共的底部
    5. </h1>
    6. {{end}}
    1. <!-- 相当于给模板定义一个名字 define end 成对出现-->
    2. {{ define "public/page_header.html" }}
    3. <h1>
    4. 我是一个公共的标题---{{.title}}
    5. </h1>
    6. {{end}}

    源码.7z