1. package main
    2. import (
    3. "net/http"
    4. "github.com/gin-gonic/gin"
    5. )
    6. func main() {
    7. // 创建一个默认的路由引擎
    8. r := gin.Default()
    9. //配置路由
    10. r.GET("/", func(c *gin.Context) {
    11. c.String(http.StatusOK, "值:%v", "你好gin")
    12. })
    13. r.GET("/news", func(c *gin.Context) {
    14. c.String(http.StatusOK, "我是新闻页面 111")
    15. })
    16. r.POST("/add", func(c *gin.Context) {
    17. c.String(http.StatusOK, "这是一个post--主要用于增加数据")
    18. })
    19. r.PUT("/edit", func(c *gin.Context) {
    20. c.String(200, "这是一个put请求 主要用于编辑数据")
    21. })
    22. r.DELETE("/delete", func(c *gin.Context) {
    23. c.String(200, "这是一个DELETE请求 用于删除数据")
    24. })
    25. // r.Run() 启动 HTTP 服务,默认在 0.0.0.0:8080 启动服务
    26. r.Run(":8000") //启动一个web服务
    27. }

    1. package main
    2. import (
    3. "net/http"
    4. "github.com/gin-gonic/gin"
    5. )
    6. type Article struct {
    7. Title string `json:"title"`
    8. Desc string `json:"desc"`
    9. Content string `json:"content"`
    10. }
    11. func main() {
    12. r := gin.Default()
    13. //配置模板的文件
    14. r.LoadHTMLGlob("templates/*")
    15. r.GET("/", func(c *gin.Context) {
    16. c.String(200, "值:%v", "首页")
    17. })
    18. r.GET("/json1", func(c *gin.Context) {
    19. c.JSON(200, map[string]interface{}{
    20. "success": true,
    21. "msg": "你好gin",
    22. })
    23. })
    24. r.GET("/json2", func(c *gin.Context) {
    25. c.JSON(200, gin.H{
    26. "success": true,
    27. "msg": "你好gin--22",
    28. })
    29. })
    30. r.GET("/json3", func(c *gin.Context) {
    31. a := &Article{
    32. Title: "我是一个标题",
    33. Desc: "描述",
    34. Content: "测试内容",
    35. }
    36. c.JSON(200, a)
    37. })
    38. //响应Jsonp请求
    39. // http://localhost:8080/jsonp?callback=xxxx
    40. // xxxx({"title":"我是一个标题-jsonp","desc":"描述","content":"测试内容"});
    41. r.GET("/jsonp", func(c *gin.Context) {
    42. a := &Article{
    43. Title: "我是一个标题-jsonp",
    44. Desc: "描述",
    45. Content: "测试内容",
    46. }
    47. c.JSONP(200, a)
    48. })
    49. r.GET("/xml", func(c *gin.Context) {
    50. c.XML(http.StatusOK, gin.H{
    51. "success": true,
    52. "msg": "你好gin 我是一个xml",
    53. })
    54. })
    55. r.GET("/news", func(c *gin.Context) {
    56. //r.LoadHTMLGlob("templates/*")
    57. c.HTML(http.StatusOK, "news.html", gin.H{
    58. "title": "我是后台的数据",
    59. })
    60. })
    61. r.GET("/goods", func(c *gin.Context) {
    62. c.HTML(http.StatusOK, "goods.html", gin.H{
    63. "title": "我是商品页面",
    64. "price": 20,
    65. })
    66. })
    67. r.Run()
    68. }
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. </head>
    9. <body>
    10. <h2>{{.title}}</h2>
    11. <br>
    12. {{.price}}
    13. <br>
    14. <h5>html的数据</h5>
    15. </body>
    16. </html>
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. </head>
    9. <body>
    10. <h2>{{.title}}</h2>
    11. </body>
    12. </html>

    1. package main
    2. import (
    3. "net/http"
    4. "github.com/gin-gonic/gin"
    5. )
    6. type Article struct {
    7. Title string
    8. Content string
    9. }
    10. func main() {
    11. r := gin.Default()
    12. //加载模板
    13. r.LoadHTMLGlob("templates/*")
    14. r.GET("/", func(c *gin.Context) {
    15. c.HTML(http.StatusOK, "index.html", gin.H{
    16. "title": "首页",
    17. })
    18. })
    19. r.GET("/news", func(c *gin.Context) {
    20. news := &Article{
    21. Title: "新闻标题",
    22. Content: "新闻详情",
    23. }
    24. c.HTML(http.StatusOK, "news.html", gin.H{
    25. "title": "新闻页面",
    26. "news": news,
    27. })
    28. })
    29. r.Run()
    30. }
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. </head>
    9. <body>
    10. <h2>{{.title}}</h2>
    11. </body>
    12. </html>
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. </head>
    9. <body>
    10. <h2>{{.title}}</h2>
    11. <p>
    12. {{.news.Title}}
    13. </p>
    14. <p>
    15. {{.news.Content}}
    16. </p>
    17. </body>
    18. </html>

    1. package main
    2. import (
    3. "net/http"
    4. "github.com/gin-gonic/gin"
    5. )
    6. type Article struct {
    7. Title string
    8. Content string
    9. }
    10. func main() {
    11. r := gin.Default()
    12. //加载模板 放在配置路由上面
    13. r.LoadHTMLGlob("templates/**/*")
    14. //前台
    15. r.GET("/", func(c *gin.Context) {
    16. c.HTML(http.StatusOK, "default/index.html", gin.H{
    17. "title": "首页",
    18. "msg": " 我是msg",
    19. "score": 89,
    20. "hobby": []string{"吃饭", "睡觉", "写代码"},
    21. "newsList": []interface{}{
    22. &Article{
    23. Title: "新闻标题111",
    24. Content: "新闻详情111",
    25. },
    26. &Article{
    27. Title: "新闻标题222",
    28. Content: "新闻详情222",
    29. },
    30. },
    31. "testSlice": []string{},
    32. "news": &Article{
    33. Title: "新闻标题",
    34. Content: "新闻内容",
    35. },
    36. })
    37. })
    38. r.GET("/news", func(c *gin.Context) {
    39. news := &Article{
    40. Title: "新闻标题",
    41. Content: "新闻详情",
    42. }
    43. c.HTML(http.StatusOK, "default/news.html", gin.H{
    44. "title": "新闻页面",
    45. "news": news,
    46. })
    47. })
    48. //后台
    49. r.GET("/admin", func(c *gin.Context) {
    50. c.HTML(http.StatusOK, "admin/index.html", gin.H{
    51. "title": "后台首页",
    52. })
    53. })
    54. r.GET("/admin/news", func(c *gin.Context) {
    55. c.HTML(http.StatusOK, "admin/news.html", gin.H{
    56. "title": "新闻页面",
    57. })
    58. })
    59. r.Run()
    60. }
    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. </head>
    11. <body>
    12. <h2>{{.title}}</h2>
    13. <!-- 定义变量 -->
    14. {{$t := .title}}
    15. <br>
    16. <h4>
    17. {{$t}}
    18. </h4>
    19. <!-- 条件判断 -->
    20. {{if ge .score 60}}
    21. <p>及格</p>
    22. {{else}}
    23. <p>不及格</p>
    24. {{end}}
    25. {{if gt .score 90}}
    26. <p>优秀</p>
    27. {{else if gt .score 80}}
    28. <p>良好</p>
    29. {{else if gt .score 60}}
    30. <p>及格</p>
    31. {{else}}
    32. <p>不及格</p>
    33. {{end}}
    34. <!-- 循环遍历数据 -->
    35. <ul>
    36. {{range $key,$value:=.hobby}}
    37. <li>{{$key}}----{{$value}}</li>
    38. {{end}}
    39. </ul>
    40. <br>
    41. <ul>
    42. {{range $key,$value:=.newsList}}
    43. <li>{{$key}}----{{$value.Title}}---{{$value.Content}}</li>
    44. {{end}}
    45. </ul>
    46. <br>
    47. <ul>
    48. {{range $key,$value:=.testSlice}}
    49. <li>{{$key}}----{{$value}}</li>
    50. {{else}}
    51. <li>数组中没有数据</li>
    52. {{end}}
    53. </ul>
    54. <!-- with 解构结构体 -->
    55. <p>{{.news.Title}}</p>
    56. <p>{{.news.Content}}</p>
    57. <br>
    58. {{with .news}}
    59. <p>{{.Title}}</p>
    60. <p>{{.Content}}</p>
    61. {{end}}
    62. </body>
    63. </html>
    64. {{ end }}
    1. <!-- 相当于给模板定义一个名字 define end 成对出现-->
    2. {{ define "default/news.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. </head>
    11. <body>
    12. <h2>{{.title}}</h2>
    13. <p>
    14. {{.news.Title}}
    15. </p>
    16. <p>
    17. {{.news.Content}}
    18. </p>
    19. </body>
    20. </html>
    21. {{end}}

    1. package main
    2. import (
    3. "fmt"
    4. "html/template"
    5. "net/http"
    6. "time"
    7. "github.com/gin-gonic/gin"
    8. )
    9. type Article struct {
    10. Title string
    11. Content string
    12. }
    13. //时间戳转换成日期
    14. func UnixToTime(timestamp int) string {
    15. fmt.Println(timestamp)
    16. t := time.Unix(int64(timestamp), 0)
    17. return t.Format("2006-01-02 15:04:05")
    18. }
    19. func Println(str1 string, str2 string) string {
    20. fmt.Println(str1, str2)
    21. return str1 + "----" + str2
    22. }
    23. func main() {
    24. // 创建一个默认的路由引擎
    25. r := gin.Default()
    26. //自定义模板函数 注意要把这个函数放在加载模板前
    27. r.SetFuncMap(template.FuncMap{
    28. "UnixToTime": UnixToTime,
    29. "Println": Println,
    30. })
    31. //加载模板 放在配置路由上面
    32. r.LoadHTMLGlob("templates/**/*")
    33. //配置静态web目录 第一个参数表示路由, 第二个参数表示映射的目录
    34. r.Static("/static", "./static")
    35. //前台
    36. r.GET("/", func(c *gin.Context) {
    37. c.HTML(http.StatusOK, "default/index.html", gin.H{
    38. "title": "aaa",
    39. "msg": " 我是msg",
    40. "score": 89,
    41. "hobby": []string{"吃饭", "睡觉", "写代码"},
    42. "newsList": []interface{}{
    43. &Article{
    44. Title: "新闻标题111",
    45. Content: "新闻详情111",
    46. },
    47. &Article{
    48. Title: "新闻标题222",
    49. Content: "新闻详情222",
    50. },
    51. },
    52. "testSlice": []string{},
    53. "news": &Article{
    54. Title: "新闻标题",
    55. Content: "新闻内容",
    56. },
    57. "date": 1629423555,
    58. })
    59. })
    60. r.GET("/news", func(c *gin.Context) {
    61. news := &Article{
    62. Title: "新闻标题",
    63. Content: "新闻详情",
    64. }
    65. c.HTML(http.StatusOK, "default/news.html", gin.H{
    66. "title": "新闻页面",
    67. "news": news,
    68. })
    69. })
    70. //后台
    71. r.GET("/admin", func(c *gin.Context) {
    72. c.HTML(http.StatusOK, "admin/index.html", gin.H{
    73. "title": "后台首页",
    74. })
    75. })
    76. r.GET("/admin/news", func(c *gin.Context) {
    77. c.HTML(http.StatusOK, "admin/news.html", gin.H{
    78. "title": "新闻页面",
    79. })
    80. })
    81. r.Run()
    82. }
    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}}
    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. {{template "public/page_header.html" .}}
    14. <img src="/static/images/node.jpg" alt="">
    15. <h2>{{.title}}</h2>
    16. <!-- 定义变量 -->
    17. {{$t := .title}}
    18. <br>
    19. <h4>
    20. {{$t}}
    21. </h4>
    22. <!-- 条件判断 -->
    23. {{if ge .score 60}}
    24. <p>及格</p>
    25. {{else}}
    26. <p>不及格</p>
    27. {{end}}
    28. {{if gt .score 90}}
    29. <p>优秀</p>
    30. {{else if gt .score 80}}
    31. <p>良好</p>
    32. {{else if gt .score 60}}
    33. <p>及格</p>
    34. {{else}}
    35. <p>不及格</p>
    36. {{end}}
    37. <!-- 循环遍历数据 -->
    38. <ul>
    39. {{range $key,$value:=.hobby}}
    40. <li>{{$key}}----{{$value}}</li>
    41. {{end}}
    42. </ul>
    43. <br>
    44. <ul>
    45. {{range $key,$value:=.newsList}}
    46. <li>{{$key}}----{{$value.Title}}---{{$value.Content}}</li>
    47. {{end}}
    48. </ul>
    49. <br>
    50. <ul>
    51. {{range $key,$value:=.testSlice}}
    52. <li>{{$key}}----{{$value}}</li>
    53. {{else}}
    54. <li>数组中没有数据</li>
    55. {{end}}
    56. </ul>
    57. <!-- with 解构结构体 -->
    58. <p>{{.news.Title}}</p>
    59. <p>{{.news.Content}}</p>
    60. <br>
    61. {{with .news}}
    62. <p>{{.Title}}</p>
    63. <p>{{.Content}}</p>
    64. {{end}}
    65. <br>
    66. <!-- 预定义函数 (了解) -->
    67. {{len .title}}
    68. <br>
    69. <br>
    70. <!-- 自定义模板函数 -->
    71. {{.date}}
    72. <br>
    73. <br>
    74. {{UnixToTime .date}}
    75. <br>
    76. <br>
    77. {{Println .title .msg}}
    78. <br> <br>
    79. {{template "public/page_footer.html" .}}
    80. </body>
    81. </html>
    82. {{ end }}
    1. <!-- 相当于给模板定义一个名字 define end 成对出现-->
    2. {{ define "default/news.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. {{template "public/page_header.html" .}}
    14. <h2>{{.title}}</h2>
    15. <p>
    16. {{.news.Title}}
    17. </p>
    18. <p>
    19. {{.news.Content}}
    20. </p>
    21. <br>
    22. <br>
    23. {{template "public/page_footer.html" .}}
    24. </body>
    25. </html>
    26. {{end}}